Go使用Text和HTML模板
来源:互联网 发布:树莓派 php无法安装 编辑:程序博客网 时间:2024/06/06 17:04
简介
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)// 执行模板,将结果写入wrfunc (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) *Templatetype FuncMap map[string]interface{}
输入
例子
最基本的使用
package mainimport ( "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 mainimport ( "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 mainimport ( "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 mainimport ( "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 mainimport ( "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 mainimport ( "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"))}
1 0
- Go使用Text和HTML模板
- Go 文本和HTML模板
- go text/templete模板
- Jquery text() 和html使用
- go的html模板替换
- .html(),.text()和.val()的使用
- .html(),.text()和.val()的使用
- .html(),.text()和.val()的使用
- jQuery使用html()和text()的区别
- go语言的模板,text/template包
- go语言的模板,text/template包
- Go 模板使用
- jquery text()和html()
- html()和text()区别
- html()和text()区别
- .html(),.text()和.val()
- jquery中html()、text()、val()的使用和区别
- 『jQuery』.html(),.text()和.val()的使用
- HT7A6312—— 离线开关电源小功率初级转换开关IC 记录总结
- 【读书笔记】机器学习实战-2.2节
- 《一条狗的使命》观后感
- PHP 常量、PHP 变量全解析(超全局变量、变量的8种数据类型等)
- 外面的世界很精彩,然而等待你的人却可能已不在
- Go使用Text和HTML模板
- 反激变压器安匝比
- 机器学习笔记6——支持向量机
- 《将博客搬至CSDN》
- 可控硅设计经验总结
- Java finalize方法使用
- fragment hint show 方法 怎么刷新
- 设计模式之状态模式
- Java中finalize()用法