草庐IT

render_template

全部标签

templates - 访问模板中包含空格的字段

我想弄清楚如何在Go中访问包含空格的模板中的map字段。但我似乎无法弄清楚..我已经从另一个我无法控制的来源解码了一个JSON数组:varf[]interface{}json.Unmarshal(externalData,&f)然后我将它传递给ExecuteTemplate,如下所示:templates.ExecuteTemplate(w,"templates/data.html",map[string]interface{}{"Data":f})在我的模板中,我使用了这个:{{range$element:=.Data}}{{$element.Name}}{{$element.**So

templates - 如何使用文本/模板包评估字段?走

我有一个模板,我想使用text/template评估各个字段包裹。我很难弄清楚评估应该如何工作,因为下面的代码似乎失败了。模板包是否足够强大以处理此类评估?typesomethingstruct{Brandstring}tpl:=`{{if.Brand=="Coke"}}It'sacoke{{else}}It'ssomethingelse{{end}}` 最佳答案 模板包中有一个名为eq的全局函数,您可以调用它。不知道为什么会这样,但这是代码typesomethingstruct{Brandstring}tpl:=`{{ifeq.B

templates - 带有 mustache 的未排序列表的结构 slice

我有一个结构。typeDataKeystruct{Idint64`db:"id"`UserIdstring`db:"user_id"`Datastring`db:"data"`CreatedAttime.Time`db:"created_at"`}我创建了一片结构。data:=[]DataKey{}在执行sql查询并填充slice后,我尝试传递给mustache建立我的list。mustache.RenderFileInLayout("templates/datakeys.html.mustache","templates/layout.html.mustache",user,data

go - 无法使用 Docker 容器自动运行简单的 Go Web 服务器(func (*Template) Execute)

所以我试图在CoreOS上的docker容器中自动运行一个简单的“helloworld”网络服务器。但是当应用程序尝试执行HTML模板时出现错误。这是有问题的代码:funcGateHandler(whttp.ResponseWriter,r*http.Request){fmt.Println("EnteredtheGateHandler.")t,_:=template.ParseFiles("templates/helloworld.html")fmt.Println("PassedtheParseFiles.")err:=t.Execute(w,nil)fmt.Println("Pa

templates - 如何使用 Go 模板在单个语句中使用多个参数

我正在尝试在单个if语句(使用text/template包)中传递多个条件,该语句应转换为“If$total==1andhasfunctionreturnsfalsedisplayworks”。我不明白这些管道究竟是如何工作的,也不明白为什么我会收到这个无意义的错误。据我所知,当使用链接(|)时,它将结果作为参数发送到最后一个命令(在本例中为and){{ifeq$total1|nehastrue|and}}Works{{end}}errtemplate::29:26:executing""at:wrongnumberofargsforne:want2got2

html - Go: template.ParseFiles() 不适用于 {{.active}} 但适用于 {{printf "%s".active}}

让我这样说我的html文件的主体Currentnumberofplayers:{{.active}}我的代码看起来像typepagestruct{activestring}t,_template.ParseFiles("page.html")t.Execute(w,page{active:"NoPlayersareOnline"})当我运行代码时,出现空白屏幕。当我将{{.active}}更改为{{printf"%s".active}}它有效。我总是需要包含printf吗?我想我对文档感到困惑。谢谢! 最佳答案 使active属性大

templates - 转到模板 : Currency pipe format?

我正在尝试在go模板中表示金钱。{{.现金}}但是现在,现金是1000000有没有可能让它输出1,000,000?是否有某种{{.cash|货币}}格式化程序?如果没有,我该如何获得所需的输出?谢谢。 最佳答案 您可以利用github.com/dustin/go-humanize来执行此操作。funcMap:=template.FuncMap{"comma":humanize.Comma,}t:=template.New("").Funcs(templateFuncs).Parse(`Amillion:{{comma.}}`)err

go - go 'Template.Execute'如何读取其匿名结构的参数字段?

在使用Go编码时,使用html.template包,调用template.Execute。我有一个问题,它如何读取其参数的匿名结构字段。看了源码,没看懂。我不知道。/usr/local/go/src/html/template/tempalte.goL.78typeTemplatestruct{...text*template.Template...}//escapeescapesallassociatedtemplates.func(t*Template)escape()error{t.nameSpace.mu.Lock()defert.nameSpace.mu.Unlock()if

html - golang html/template ExecuteTemplate errant byte 它来自哪里?

背景我试图为Go编写一些GZIP中间件,但遇到了一个问题,因为http.DetectContentType()返回的是text/plain而不是text/html所以我追踪到第一个被写入的文本是某种类型或错误的字节,我仍在努力追查。我知道有很多解决方法,比如在执行模板时显式设置ContentType或使用bytes.Buffer并一次写入所有内容,但我真的很想找出导致写入这个单个字节的原因。例子这是一个非常人为的例子,它显示了同样的问题https://play.golang.org/p/SSrWP9jLRq运行时你会看到打印的第一件事是:“LENBYTES:1String:”嫌疑人如示

go - template.ParseFiles 的问题

我有以下http.Handle函数(简化):funcloginHandler(whttp.ResponseWriter,r*http.Request){cwd,_:=os.Getwd()t,err:=template.ParseFiles(filepath.Join(cwd,"./views/login.html"))iferr!=nil{fmt.Fprintf(w,"503-Error")fmt.Println(err)}else{t.Execute(w,nil)}}它在使用gobuildmain.go时按预期工作,但是-在运行goinstall之后,我得到一个找不到文件的错误(因为