如何将一个变量(而不是结构体成员)传递到 text/html 模板中。Golang

huangapple go评论73阅读模式
英文:

How to pass just a variable (not a struct member) into text/html template. Golang

问题

有没有办法将变量(字符串、整数、布尔值)传递到模板中?例如(类似的方式):

import (
    "html/template"
)

func main() {
    ....
    tmpl := template.Must(template.ParseFiles("templates/index.html"))
    mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
        varmap := map[string]interface{}{
            "var1": "value",
            "var2": 100,
        }
        tmpl.ExecuteTemplate(rw, "index", varmap)
    })
    
    // index.html的内容
    {{define "index"}}
    {{var1}} 等于 {{var2}}
    {{end}}
}
英文:

is there any way to pass just a variable (string, int, bool) into template. For example (something similar):

import (
    "html/template"
)

func main() {
    ....
	tmpl := template.Must(template.ParseFiles("templates/index.html"))
	mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
		varmap := map[string]interface{}{
			"var1": "value",
			"var2": 100,
		}
		tmpl.ExecuteTemplate(rw, "index", varmap)
	})
	
    // content of index.html
	{{define "index"}}
	{{var1}} is equal to {{var2}}
	{{end}}
}

答案1

得分: 14

是的,只需在其前面使用点号:

http://play.golang.org/p/7NXu9SDiik

package main

import (
	"html/template"
	"log"
	"os"
)

var tmplString = `    // content of index.html
    {{define "index"}}
    {{.var1}} 等于 {{.var2}}
    {{end}}
`

func main() {
	tmpl, err := template.New("test").Parse(tmplString)
	if err != nil {
		log.Fatal(err)
	}
	varmap := map[string]interface{}{
		"var1": "value",
		"var2": 100,
	}
	tmpl.ExecuteTemplate(os.Stdout, "index", varmap)

}
英文:

Yes just use the dot in front of it:

http://play.golang.org/p/7NXu9SDiik

package main

import (
	"html/template"
	"log"
	"os"
)

var tmplString = `    // content of index.html
    {{define "index"}}
    {{.var1}} is equal to {{.var2}}
    {{end}}
`

func main() {
	tmpl, err := template.New("test").Parse(tmplString)
	if err != nil {
		log.Fatal(err)
	}
	varmap := map[string]interface{}{
		"var1": "value",
		"var2": 100,
	}
	tmpl.ExecuteTemplate(os.Stdout, "index", varmap)

}

答案2

得分: 8

可以传递任意类型的简单值。如果这样做,你可以在模板中使用{{.}}来引用它。

以下是一个示例(在Go Playground上尝试):

s := "<html><body>Value passed: {{.}}</body></html>\n"

tmpl := template.Must(template.New("test").Parse(s))

tmpl.Execute(os.Stdout, false)
tmpl.Execute(os.Stdout, 1)
tmpl.Execute(os.Stdout, "me")

输出结果:

<html><body>Value passed: false</body></html>
<html><body>Value passed: 1</body></html>
<html><body>Value passed: me</body></html>
英文:

It is possible to pass just a simple value of any type. If you do so, you can refer to it in the template as {{.}}.

Here is an example (try it on the Go Playgound):

s := &quot;&lt;html&gt;&lt;body&gt;Value passed: {{.}}&lt;/body&gt;&lt;/html&gt;\n&quot;

tmpl := template.Must(template.New(&quot;test&quot;).Parse(s))

tmpl.Execute(os.Stdout, false)
tmpl.Execute(os.Stdout, 1)
tmpl.Execute(os.Stdout, &quot;me&quot;)

Output:

&lt;html&gt;&lt;body&gt;Value passed: false&lt;/body&gt;&lt;/html&gt;
&lt;html&gt;&lt;body&gt;Value passed: 1&lt;/body&gt;&lt;/html&gt;
&lt;html&gt;&lt;body&gt;Value passed: me&lt;/body&gt;&lt;/html&gt;

答案3

得分: 0

尝试使用fasttemplate 1。它接受一个占位符替换值的映射。而且在简单模板上的速度比html/template更快。

1 http://github.com/valyala/fasttemplate

英文:

Try fasttemplate 1. It accepts a map of placeholders' substitution values. And it works faster than html/template on simple templates.

1 http://github.com/valyala/fasttemplate

huangapple
  • 本文由 发表于 2015年1月16日 03:16:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/27971240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定