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时,调用panic。Must的引入,说的不好听点,就是为了中和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
- Golang Template
- golang template
- golang中的模板template
- Golang简单的template使用
- Golang template简单实现分页
- golang 模板(test/template)
- golang工具基础——xml、template
- golang的template实现自定义的循环
- Golang Template source code analysis(Parse)
- golang 模板(template)的常用基本语法
- golang 模板(template)的常用基本语法
- golang 模板(template)的常用基本语法
- 利用golang的template模板包进行web开发
- golang text/template的用法: if, else, with
- GoLang 遍历 map、slice、array方法和template遍历map
- template
- Template
- template
- codeforces 675c Money Transfers
- apache多站点配置
- Shiro系列教程URL匹配规则(AntPathMatcher)
- ubuntu 16.04 git 服务器安装
- git clone 命令行http代理设置
- Golang Template
- win10下Chrome安装位置调整
- 第十一天2017/04/25(1、二叉树)
- 商品展示案例
- 797A
- HTML5做移动开发一定要搞明白MPA 与 SPA 的差别
- xml实现卡片效果
- Oracle模糊查询优化
- ThinkPHP的使用(五)富文本编辑器的使用以及防xss攻击