golang template
来源:互联网 发布:西安网络推广外包公司 编辑:程序博客网 时间:2024/06/06 01:55
关键内容:
{{.}} 表示当前对象 --直接输出对象便利店数组 map 数据切片{{range …}}{{end}} {{with …}}…{{end}} package mainimport ( "html/template" "os")type Friend struct { Fname string}type Person struct { UserName string Emails []string Friends []*Friend}func main() { f1 := Friend{Fname: "xiaomig"} f2 := Friend{Fname: "xiaowei"} t := template.New("fieldname example") t, _ = t.Parse(`hello {{.UserName}}! {{range .Emails}} an email {{.}} {{end}} {{with .Friends}} {{range .}} my friend name is {{.Fname}} {{end}} {{end}} `) p := Person{UserName: "Astaxie", Emails: []string{"qinzhao@qinzhao.com", "qinzhao@qinzhaocom1"}, Friends: []*Friend{&f1, &f2}} t.Execute(os.Stdout, p)}
条件处理
if-else语法
如果if else
package mainimport ( "os" "text/template")func main() { tEmpty := template.New("template test") tEmpty = template.Must(tEmpty.Parse("空 pipeline if demo: {{if ``}} 不会输出. {{end}}\n")) tEmpty.Execute(os.Stdout, nil) tWithValue := template.New("template test") tWithValue = template.Must(tWithValue.Parse("不为空的 pipeline if demo: {{if `anything`}} 我有内容,我会输出. {{end}}\n")) tWithValue.Execute(os.Stdout, nil) tIfElse := template.New("template test") tIfElse = template.Must(tIfElse.Parse("if-else demo: {{if `anything`}} if部分 {{else}} else部分.{{end}}\n")) tIfElse.Execute(os.Stdout, nil)}*if里面只能是bool值
pipelines
{{. | html}}
模板变量
$var := piplepackage mainimport ( "text/template" "fmt" "os")func main() { data:=`{{with $x := "qinzhao" | printf "%q"}}{{$x}}{{end}}{{with $x := "output"}}{{printf "%q" $x}}{{end}}{{with $x := "output"}}{{$x | printf "%q"}}{{end}}` t:=template.New("demo04") tpl,err:=t.Parse(data) if err!=nil{ fmt.Printf("template parse failed %#v \n",err) return } tpl.Execute(os.Stdout,nil) println()}
模板函数
package mainimport ( "fmt" "html/template" "os" "strings")type Friend struct { Fname string}type Person struct { UserName string Emails []string Friends []*Friend}func EmailDealWith(args ...interface{}) string { ok := false var s string if len(args) == 1 { s, ok = args[0].(string) } if !ok { s = fmt.Sprint(args...) } // find the @ symbol substrs := strings.Split(s, "@") if len(substrs) != 2 { return s } // replace the @ by " at " return (substrs[0] + " at " + substrs[1])}func main() { f1 := Friend{Fname: "xiaoming"} f2 := Friend{Fname: "hello"} t := template.New("fieldname example") t = t.Funcs(template.FuncMap{"emailDeal": EmailDealWith}) t, _ = t.Parse(`hello {{.UserName}}! {{$var:=.Emails | len}} {{$var}} {{range .Emails}} an emails {{.|emailDeal}} {{end}} {{with .Friends}} {{range .}} my friend name is {{.Fname}} {{end}} {{end}} `) p := Person{UserName: "qinzhao", Emails: []string{"qinzhao@ww.com", "qinzhao@ww.com01"}, Friends: []*Friend{&f1, &f2}} t.Execute(os.Stdout, p) println()}
模板包内部已经有内置的实现函数
var builtins = FuncMap{ "and": and, "call": call, "html": HTMLEscaper, "index": index, "js": JSEscaper, "len": length, "not": not, "or": or, "print": fmt.Sprint, "printf": fmt.Sprintf, "println": fmt.Sprintln, "urlquery": URLQueryEscaper,}
Must操作
package mainimport ( "fmt" "text/template")func main() { tOk := template.New("first") template.Must(tOk.Parse(" some static text /* and a comment */")) fmt.Println("The first one parsed OK.") template.Must(template.New("second").Parse("some static text {{ .Name }}")) fmt.Println("The second one parsed OK.") fmt.Println("The next one ought to fail.") tErr := template.New("check parse error with Must") template.Must(tErr.Parse(" some static text {{ .Name }}"))}
嵌套模板
{{define "子模板名称"}}内容{{end}}通过如下方式来调用{{template "子模板名称"}}//header.tmpl{{define "header"}}<html><head> <title>演示信息</title></head><body>{{end}}//content.tmpl{{define "content"}}{{template "header"}}<h1>演示嵌套</h1><ul> <li>嵌套使用define定义子模板</li> <li>调用使用template</li></ul>{{template "footer"}}{{end}}//footer.tmpl{{define "footer"}}</body></html>{{end}}
package mainimport ( "fmt" "os" "text/template")func main() { s1, _ := template.ParseFiles("header.html", "content.html", "footer.html") s1.ExecuteTemplate(os.Stdout, "header", nil) fmt.Println() s1.ExecuteTemplate(os.Stdout, "content", nil) fmt.Println() s1.ExecuteTemplate(os.Stdout, "footer", nil) fmt.Println() s1.Execute(os.Stdout, nil) println()}
else
eq Returns the boolean truth of arg1 == arg2ne Returns the boolean truth of arg1 != arg2lt Returns the boolean truth of arg1 < arg2le Returns the boolean truth of arg1 <= arg2gt Returns the boolean truth of arg1 > arg2ge Returns the boolean truth of arg1 >= arg2
阅读全文
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
- Java自学视频整理
- PDM导出sql的方法和注意事项(本人…
- 工作流学习——重要概念扫盲篇一步曲
- android获取农历的方法
- 1---不详细的讲一下Xilinx的BMG:单端口和双端口RAM的区别
- golang template
- 手写一个轻量级的网关API
- java读取Properties文件
- 安卓APP崩溃(crash)后重新启动,捕获全局异常重启APP
- java 学习
- 用react写弹出框组件跟登录组件的一些记录
- @Table标记注解
- 域名绑定和域名解析的区别
- Activiti用户任务分配