简介
Go的两个包text/template
和html/template
提供了将变量的值替换到文本的机制。通常用在一些printf
应付不了的、需要复杂的格式化的场合。
模板的使用
模板规则
Action
模板是一个包含了一个或多个双花括号标记({{...}}
)的字符串,这种双花括标记号叫做Action。模板中所有的除Action的字符串都按照原样打印,Action则表现了特殊的行为。通过Action可以打印变量的值、字段的值、调用函数或方法、表达控制流程等。
Dot
Action中的点(.
)表示当前值。初始值为传递给模板的参数。
API
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) *Template
Funcs()用来注册函数给模板,注册之后模板就可以通过名字调用外部函数了(见下面例子)。
func (t *Template) Funcs(funcMap FuncMap) *Template type FuncMap map[string]interface{}
输入
例子
最基本的使用
package main
import (
"log"
"os"
"text/template" // 引入包,如果处理HTML,则为 "html/template"
)
func main() {
// 创建一个名字为report的模板,并解析一个字符串
t, err := template.New("report").Parse("I am {{.}} years old.")
if err != nil {
log.Fatal(err)
}
// 执行模板,5作为参数传递,将结果写到标准输出
t.Execute(os.Stdout, 5)
// 打印结果:I am 5 years old.
}
访问复合数据结构的值
访问结构体字段需要在点(.
)后面加字段名。
访问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
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")) }
更多推荐
Go使用Text和HTML模板
发布评论