Go的两个包text/template和html/template提供了将变量的值替换到文本的机制。通常用在一些printf应付不了的、需要复杂的格式化的场合。
模板是一个包含了一个或多个双花括号标记({{...}})的字符串,这种双花括标记号叫做Action。模板中所有的除Action的字符串都按照原样打印,Action则表现了特殊的行为。通过Action可以打印变量的值、字段的值、调用函数或方法、表达控制流程等。
Action中的点(.)表示当前值。初始值为传递给模板的参数。
text/template与html/template提供了相同的API来创建、解析和执行模板,区别是html/template提供了HTML转义。
// 创建一个名字为name的模板 func New(name string) *Template // 解析模板字符串 func (t *Template) Parse(text string) (*Template, error) // 解析文件 func ParseFiles(filenames ...string) (*Template, error) // 执行模板,将结果写入wr func (t *Template) Execute(wr io.Writer, data interface{}) error在确定模板可以正确解析的场合,可以使用Must()来处理Parse()的结果。如果Parse()失败,则调用panic(),如果成功,返回Template对象。
func Must(t *Template, err error) *TemplateFuncs()用来注册函数给模板,注册之后模板就可以通过名字调用外部函数了(见下面例子)。
func (t *Template) Funcs(funcMap FuncMap) *Template type FuncMap map[string]interface{}输入
访问结构体字段需要在点(.)后面加字段名。 访问Map值需要在点(.)后面加Key的值。
package main import ( "os" "text/template" ) func main() { t := template.Must( template.New("report").Parse("I am {{.Age}} years old.\n")) t.Execute(os.Stdout, struct{ Age int }{5}) t.Execute(os.Stdout, map[string]int{"Age": 5}) }需要注意的是template访问结构体字段时利用了反射机制,因此必须导出字段才能在模板里访问。
使用{{range}}和{{end}}来表示循环。每次迭代中点(.)代表当前迭代的值。
package main import ( "os" "text/template" ) func main() { const temStr = "{{range .}}{{.}}\n{{end}}" t := template.Must(template.New("report").Parse(temStr)) t.Execute(os.Stdout, []string{"Hello", "Gopher"}) }用|将其左边的输出作为其右边的输入。
package main import ( "os" "text/template" ) func main() { const temStr = `{{. | printf "% 10s"}}` t := template.Must(template.New("report").Parse(temStr)) t.Execute(os.Stdout, "Gopher") }如果在模板中需要调用函数,则在Parse()之前需要通过Funcs()来注册。
package main import ( "os" "text/template" ) func greet(name string) string { return "Hello, " + name + "!" } func main() { const temStr = `{{. | greet}}` t := template.Must(template.New("report"). Funcs(template.FuncMap{"greet": greet}). Parse(temStr)) t.Execute(os.Stdout, "Gopher") }HTML文件:time.html
<html> <body> <p>Current time is: {{.}}</p> </body> </html>go文件:
package main import ( "html/template" "log" "net/http" "time" ) func main() { http.HandleFunc("/time", timeHandler) log.Fatal(http.ListenAndServe(":9090", nil)) } func timeHandler(w http.ResponseWriter, r *http.Request) { t := template.Must(template.ParseFiles("time.html")) t.Execute(w, time.Now().Format("2006-01-02 15:04:05")) }