1:{{len "aa"}} 推荐
2:{{"aa"|len}}
3:嵌套方法:{{len (substr "123456789" 2 4)}}
输出内容:
1: 遍历元素
{{ range .listStr }}
元素: {{ . }};
{{ end }}
3: 遍历元素,函数截取数组
{{range (slice .listStr 3 6) }}
元素:{{.}};
{{end}}
2: 遍历元素,带索引
{{ range $index,$item:= .listStr }}
索引{{$index}},值:{{$item}};
{{ end }}
3: 遍历元素,带索引,带索引值,函数截取数组
{{range $index,$item:= (slice .listStr 3 6) }}
索引{{$index}},值:{{$item}};
{{end}}
4: 遍历空数组,显示没有数据的内容
{{ range (slice .listStr 3 3) }}
元素:{{.}};
{{ else }}
遍历的数组没有数据
{{ end }}
4: 遍历数组,调用全局变量
{{range .listStr }}
局部变量:{{.}}__全局变量{{$.t}}
{{end}}
{{if eq .t 100}}
等于100
{{else if eq .t 200}}
等于200
{{else}}
其他
{{end}}
{{if lt .t 1}}
小于1
{{end}}
{{if le .t 0}}
小于或等于0
{{end}}
{{if gt .t -1}}
大于-1
{{end}}
{{if ge .t 0}}
大于等于0
{{end}}
{{if or (eq .t 100) (eq .t 200)}}
等于100或等于200
{{end}}
{{if and (eq .t 100) (eq .t 200)}}
等于100且等于200
{{end}}
2: 遍历元素,带索引
{{ range $index,$item:= .listStr }}
{{if eq $index 0}}
我是索引0;
{{else if or (eq $index 1) (eq $index 2)}}
我是索引1或2;
{{end}}
{{end}}