go语言模板语法

作者: 小新

发布于 2020-01-02 | 最后更新 2020-01-02


1. 基本语法

go 统一使用了 {{ 和 }} 作为左右标签,没有其他的标签符号。

使用 . 来访问当前位置的上下文

使用 $ 来引用当前模板根级的上下文

使用 $var 来访问创建的变量

模板中支持的 go 语言符号

{{"string"}} // 一般 string
{{`raw string`}} // 原始 string
{{'c'}} // byte
{{print nil}} // nil 也被支持

模板中的 pipeline

可以是上下文的变量输出,也可以是函数通过管道传递的返回值

{{. | FuncA | FuncB | FuncC}}

当 pipeline 的值等于:

false 或 0 nil 的指针或 interface 长度为 0 的 array, slice, map, string 那么这个 pipeline 被认为是空

1.1. if … else … end 

{{if pipeline}}{{end}}

if 判断时,pipeline 为空时,相当于判断为 False

支持嵌套的循环

{{if .IsHome}}
{{else}}
    {{if .IsAbout}}{{end}}
{{end}}

也可以使用 else if 进行

{{if .IsHome}}
{{else if .IsAbout}}
{{else}}
{{end}}

假设我们需要逻辑判断,比如与或、大小不等于等判断的时候,我们需要一些内置的模板函数来做这些工作,目前常用的一些内置模板函数有:

// not 非
{{if not .condition}}
{{end}}

// and 与
{{if and .condition1 .condition2}}
{{end}}

// or 或
{{if or .condition1 .condition2}}
{{end}}

// eq 等于
{{if eq .var1 .var2}}
{{end}}

// ne 不等于
{{if ne .var1 .var2}}
{{end}}

// lt 小于 (less than)
{{if lt .var1 .var2}}
{{end}}

// le 小于等于
{{if le .var1 .var2}}
{{end}}

// gt 大于
{{if gt .var1 .var2}}
{{end}}

// ge 大于等于
{{if ge .var1 .var2}}
{{end}}

1.2. range … end

{{range pipeline}}{{.}}{{end}}

pipeline 支持的类型为 array, slice, map, channel

range 循环内部的 . 改变为以上类型的子元素 对应的值长度为 0 时,range 不会执行,. 不会改变。

pages := []struct {
    Num int
}{{10}, {20}, {30}}
 
this.Data["Total"] = 100
this.Data["Pages"] = pages

使用 .Num 输出子元素的 Num 属性,使用 $. 引用模板中的根级上下文

{{range .Pages}}
    {{.Num}} of {{$.Total}}
{{end}}

使用创建的变量,在这里和 go 中的 range 用法是相同的。

{{range $index, $elem := .Pages}}
    {{$index}} - {{$elem.Num}} - {{.Num}} of {{$.Total}}
{{end}}

range 也支持 else

{{range .Pages}}
{{else}}
    {{/* 当 .Pages 为空 或者 长度为 0 时会执行这里 */}}
{{end}}

1.3. with … end

{{with pipeline}}{{end}}

with 用于重定向 pipeline

{{with .Field.NestField.SubField}}
    {{.Var}}
{{end}}

也可以对变量赋值操作

{{with $value := "My name is %s"}}
    {{printf . "slene"}}
{{end}}
with 也支持 else

{{with pipeline}}
{{else}}
    {{/* 当 pipeline 为空时会执行这里 */}}
{{end}}

1.4. define

define 可以用来定义自模板,可用于模块定义和模板嵌套

{{define "loop"}}
    <li>{{.Name}}</li>
{{end}}

使用 template 调用模板

<ul>
    {{range .Items}}
        {{template "loop" .}}
    {{end}}
</ul>

1.5. template

{{template "模板名" pipeline}}

将对应的上下文 pipeline 传给模板,才可以在模板中调用

Beego 中支持直接载入文件模板

{{template "path/to/head.html" .}}

Beego 会依据你设置的模板路径读取 head.html

在模板中可以接着载入其他模板,对于模板的分模块处理很有用处

1.6. 注释

允许多行文本注释,不允许嵌套

{{/* comment content
support new line */}}

1.7. 加载模板

gin 通过LoadHTMLGlob加载文件夹下的所有静态页面。

r := gin.Default()
r.LoadHTMLGlob("template/*") 
//注:此写法仅支持template目录下所有文件,但不包含目录。
//如:template/test.tpl

支持多级目录,如:匹配

r := gin.Default()
r.LoadHTMLGlob("template/*/*")
//注:此写法仅支持template目录下所有文件和目录,但不包含子级里的目录。
//如:template/test.tpl
//如:template/user/other.tpl

1.8. 自定义函数

golang的模板其实功能很有限,很多复杂的逻辑无法直接使用模板语法来表达,所以只能使用模板函数来绕过。

使用template.FuncMap方法:

// 显示时间
func ShowTime(format string, val time.Time, ) string {
	return val.Format(format)
}
// 主函数
func main(){
	// gin SetFuncMap
	r := gin.Default()
	c.SetFuncMap(template.FuncMap{
		"ShowTime": ShowTime,
	})
}

//模板使用
{{ . | ShowTime "2006-01-02 15:04:05" }}