Golang Template

转载: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)

文件初始化模板

template.ParseFiles("hello.txt"))
t.Execute(os.Stdout,161)">// hello.txt:
// hello world

// hello world

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

"hello")
template.Must(t.ParseFiles("hello.txt"))
"world.txt"))
t.ExecuteTemplate(os.Stdout,"world.txt",161)">// world.txt:
// another file

// another file

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

数据驱动

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

也可以传点别的

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

Map

marrage_info := map[string]bool{
    "HanMeimei": true,152)">"LiLei": false,}
t := "Married: Han Meimei:{{.HanMeimei}}; Li Lei:{{.LiLei}}"))
t.Execute(os.Stdout,marrage_info )

// Married: Han Meimei:true; Li Lei:false

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

info := bool{
"Han Meimei": true,152)">"test").Parse(`Married: Han Meimei:{{index . "Han Meimei"}}; Li Lei:{{.LiLei}}`))
t.Execute(os.Stdout,info)

// Married: Han Meimei:true; Li Lei:false

array/slice

infos := []string{"Lilei"}
t := "Students List:" +
    "{{range .}}" +
"\n{{.}}," +
"{{end}}"))
t.Execute(os.Stdout,infos)

// Students List:
// Han Meimei,
// Lilei,

自定义变量

"{{range $index,$_ := .}}" +
"\n{{$index}}. {{.}},161)">// 0. Han Meimei,161)">// 1. Lilei,47); margin:0px 0px 15px; font-size:22px"> 条件查询 
"LiLei"
t := template.Must(template.New("test").Parse(`{{if eq . "LiLei"}}Man{{else "Han Meimei"}}Women{{end}}`))
t.Execute(os.Stdout,161)">// Man

转义

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

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

比如

"<script>"
t := "{{. | urlquery}} and {{. | html}}"))
t.Execute(os.Stdout,161)">// %3Cscript%3E and <script>

模板重构

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

自定义方法

// func returnBool(b bool) bool {
// return b
// }

funcs := template.FuncMap{
"is_teacher_coming": returnBool,152)">"test").Funcs(funcs)
template.Must(t.Parse("{{if is_teacher_coming .}}Carefully!{{end}}"))
t.Execute(os.Stdout,true)

// Carefully!

嵌套模板

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

// output:
// Name: Han Meimei; Age: 30.

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

相关文章

什么是Go的接口? 接口可以说是一种类型,可以粗略的理解为他...
1、Golang指针 在介绍Golang指针隐式间接引用前,先简单说下...
1、概述 1.1&#160;Protocol buffers定义 Protocol buffe...
判断文件是否存在,需要用到"os"包中的两个函数: os.Stat(...
1、编译环境 OS :Loongnix-Server Linux release 8.3 CPU指...
1、概述 Golang是一种强类型语言,虽然在代码中经常看到i:=1...