模板变量传递(data为模板变量)
template.Execute(wr, data)
模板变量使用
{{.}}
{{.Name}}
去除空白
{{-空格 去掉左边的空白字符
空格-}} 去掉右边的空白字符
注释
{{/* */}}
{{- /* 会把两边的空白字符都去掉的注释 */ -}}
动作(Action)
{{pipeline}} 输出pipeline
{{if pipeline}} A {{end}} 当pipeline的值是0、false、nil、空数组、空切片、空map、空字符串时输出 A
{{if pipeline}} A {{else}} B {{end}}
{{if pipeline}} A {{else if pipeline}} B {{end}}
{{range pipeline}} A {{end}}
{{template "name"}}
{{template "name" pipeline}}
{{block "name" pipeline}} T1 {{end}} 定义一个模板并引入,等于 {{define "name"}} T1 {{end}} {{template "name" pipeline}}
{{with pipeline}} T1 {{end}} 将pipeline的值赋值给.
{{with pipeline}} T1 {{else}} T0 {{end}}
参数(argument)
常量,类型支持 boolean, string, char, int, float, imag, complex
关键字 nil
点 .
变量名 $varname
结构属性 .Field
map的元素值 .Key
无参方法 .Method
无参函数 funcname
小括号分隔多个函数、方法 print (.F1 arg1) (.F2 arg2)
pipeline
参数
.Method 参数...
Func 参数...
支持多个之间使用 | 连接,前一个pipeline的结果作为后一个pipeline的最后参数
变量
$varname := pipeline 定义
$varname = pipeline 重新赋值
range $index, $element := pipeline 最初的模板变量会赋给 $
函数
and 例 and x y 等于 if x then y eles x
call 将第一个参数当作函数调用 call .X.Y 1 2 等于 .X.Y(1, 2),Y是个函数类型字段
html 返回html转义后的内容(html/template包不可用)
index 将第二个参数开始的所有参数当成是第一个参数的索引返回值,例 index x 1 2 3 等于 x[1][2][3]
slice 切片操作 slice x 等于 slice [:],slice x 1 等于 x[1:],slice x 1 3 等于 x[1:3]
js 返回js转义后的
len 返回长度
not 返回非值
or 或,ox x y 等于 if x then x else y
printf 等于 fmt.Srintf
printf 等于fmt.Sprintf
println 等于fmt.Sprintln
urlquery 返回url编码后的内容(html/template包不可用)
二元函数
eq 等于
ne 不等于
lt 小于
le 小于等于
gt 大于
ge 大于等于
嵌套模板定义
`{{define "T1"}}ONE{{END}}`