go html模板引擎,Go template模板

模板

关键点

text and spaces : 静态和动态数据

actions

arguments : 动态数据来源

pipeline : 操作数据

variables

functions :预先全局存在可以直接用 add、call、html、index、slice、print

asccociated template

nested template defintions

glob :通过类似正则匹配,一次渲染多个template

核心

动态数据如何表示 (基本类型、复合类型、作用域{{end}}前)

如何操作数据(流程控制、循环处理、函数方法操作、嵌套)

语法

pipelines

A pipeline is a possibly chained sequence of "commands". 命名链条

A command is a simple value (argument) or a function or method call, possibly with multiple arguments.

# 渲染数据可以是go的所有类型

# 渲染数据本身

{{ . }}

# 自定义变量

{{ $var = . }}

# struct

# 大写字母开头属性Field

{{ .Field }}

# 嵌套struct时的 属性Field1

{{ .Field.Field1 }}

# 变量为struct时的 属性Field

{{ $x = . }}

{{ $x.Field }}

# 方法

{{ .Method }}

# 嵌套struct的方法

{{ .Field.Method }}

# 嵌套struct的map中struct的方法

{{ .Field.Key.Method }}

# map

# 键名

{{ .key }}

# struct中map

{{ .Field.key }}

# 变量的struct中map

{{ $x = . }}

{{ $x.Field.key }}

# 函数

{{ funName . }}

操作

# 注释

{{/* comment */}}

{{- /* comment */ -}}

# 默认输出,效果 fmt.Print(pipeline)

{{ pipeline }}

# 流程控制

{{if pipeline}} T1 {{end}}

{{if pipeline}} T1 {{else}} T0 {{end}}

{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}

{{with pipeline}} T1 {{end}}

{{with pipeline}} T1 {{else}} T0 {{end}}

# 循环

# array, slice, map, or channel

{{range pipeline}} T1 {{end}}

{{range pipeline}} T1 {{else}} T0 {{end}}

# 嵌套关联

{{template "name"}}

# 当前模板引入其他模板,并且传递数据

{{template "name" pipeline}}

# 等价声明和执行 {{define "name"}} T1 { {end}} & {{template "name" pipeline}}

{{block "name" pipeline}} T1 {{end}}

例子

待处理列表

序号说明

# []*struct 遍历slice的struct赋值给变量

{{ range $key, $value := .}}

{{ $key }}

# 判断struct的属性Priority, 为Description标记不通背景颜色

{{ if eq $value.Priority 4 }}

{{ $value.Description }}

{{ else if eq $value.Priority 3 }}

{{ $value.Description }}

{{ else if eq $value.Priority 2 }}

{{ $value.Description }}

{{ end }}

{{end}}

使用

可以传递函数function

可以直接解析模板文件.tmpl或.html

可以glob同时解析过个模板文件

func main() {

stu := struct{Name string, ID int}{Name: "hello", ID: 11}

// 创建模板对象, parse关联模板

tmpl, err := template.New("test").Parse("{{.Name}} ID is {{ .ID }}")

if err != nil {

panic(err)

}

// 渲染stu为动态数据, 标准输出到终端

err = tmpl.Execute(os.Stdout, stu)

if err != nil {

panic(err)

}

}

// output

// hello ID is 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值