Golang Template

来源:互联网 发布:手机光照度测试软件 编辑:程序博客网 时间:2024/06/15 23:50

 转载:http://www.jianshu.com/p/bee02c18b221


最近刚好有个task是要用Golang把Junit的XML格式report转换成HTML格式,便学习了Golang的template包。

基于template做的那个tool transforming Junit XML report to HTML.

Golang提供了对模板的支持(按照文档的说法,是数据驱动模板,data-driven template),分别在"text/template"和"html/template"两个包下。这两个包在api级别是一致的,只是"html/template"提供了对html文本的更好支持,比如会将一些html中的关键符号(类似'<', '>'之类的)做些转义处理再输出。所以以下就只对"html/template"做下介绍。

模板定义

hello world

t := template.Must(template.New("hello").Parse("hello world"))t.Execute(os.Stdout, nil)// output:// hello

此处,template.Must(*template.Template, error )会在Parse返回err不为nil时,调用panicMust的引入,说的不好听点,就是为了中和Golang将error置于函数返回值这种做法带来的缺点。

有了Must,我们可以将两句inline在一起。

template.Must(template.New("hello").Parse("hello world")).Execute(os.Stdout, nil)

从文件初始化模板

t := template.Must(template.ParseFiles("hello.txt"))t.Execute(os.Stdout, nil)// hello.txt:// hello world// output:// hello world

通过文件名字,指定对应的模板

t := template.New("hello")template.Must(t.ParseFiles("hello.txt"))template.Must(t.ParseFiles("world.txt"))t.ExecuteTemplate(os.Stdout, "world.txt", nil)// world.txt:// another file// output:// another file

所以,一个template实例,其实是一个模板的集合,我们可以为每个模板命名。

数据驱动

hello world

s := "LiLei"t := template.Must(template.New("test").Parse("Watch out, {{.}}!"))t.Execute(os.Stdout, s)// output:// Watch out, LiLei!

也可以传点别的

s := &student{Name: "Han Meimei", Age: 30}t := template.Must(template.New("test").Parse("{{.Name}} looks like more than {{.Age}} years old!"))t.Execute(os.Stdout, s)// output:// Han Meimei looks like more than 30 years old!

Map

marrage_info := map[string]bool{    "HanMeimei": true,    "LiLei": false,}t := template.Must(template.New("test").Parse("Married: Han Meimei:{{.HanMeimei}}; Li Lei:{{.LiLei}}"))t.Execute(os.Stdout, marrage_info )// output:// Married: Han Meimei:true; Li Lei:false

在template中key有空格,得用到index

info := map[string]bool{"Han Meimei": true,"LiLei": false,}t := template.Must(template.New("test").Parse(`Married: Han Meimei:{{index . "Han Meimei"}}; Li Lei:{{.LiLei}}`))t.Execute(os.Stdout, info)// output:// Married: Han Meimei:true; Li Lei:false

array/slice

infos := []string{"Han Meimei", "Lilei"}t := template.Must(template.New("test").Parse("Students List:" +    "{{range .}}" +"\n{{.}}," +"{{end}}"))t.Execute(os.Stdout, infos)// output:// Students List:// Han Meimei,// Lilei,

自定义变量

infos := []string{"Han Meimei", "Lilei"}t := template.Must(template.New("test").Parse("Students List:" +    "{{range $index, $_ := .}}" +"\n{{$index}}. {{.}}," +"{{end}}"))t.Execute(os.Stdout, infos)// output:// Students List:// 0. Han Meimei,// 1. Lilei,

条件查询

s := "LiLei"t := template.Must(template.New("test").Parse(`{{if eq . "LiLei"}}Man{{else if eq . "Han Meimei"}}Women{{end}}`))t.Execute(os.Stdout, s)// output:// Man

转义

在"html/template"中,如下两端代码是等价的。

<a href="/search?q={{.}}">{{.}}</a>
<a href="/search?q={{. | urlquery}}">{{. | html}}</a>

比如

s := "<script>"t := template.Must(template.New("test").Parse("{{. | urlquery}} and {{. | html}}"))t.Execute(os.Stdout, s)// output:// %3Cscript%3E and <script>

模板重构

Golang提供的几个功能,为模板的重构提供了更多的可能。

自定义方法

// func returnBool(b bool) bool {//     return b// }funcs := template.FuncMap{"is_teacher_coming": returnBool,}t := template.New("test").Funcs(funcs)template.Must(t.Parse("{{if is_teacher_coming .}}Carefully!{{end}}"))t.Execute(os.Stdout, true)// output:// Carefully!

嵌套模板

s := &student{Name: "Han Meimei", Age: 30}t := template.New("test")template.Must(t.Parse(`Name: {{.Name}}; {{template "age" .Age}}.`))// another way to name a templatetemplate.Must(t.Parse(`{{define "age"}}Age: {{.}}{{end}}`))t.Execute(os.Stdout, s)// output:// Name: Han Meimei; Age: 30.

这里也提供了另外一个方法去指定一个模板的名字。


0 0
原创粉丝点击