🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
模板语法 go 统一使用了 {{ 和 }} 作为左右标签,没有其他的标签符号。 使用 . 来访问当前位置的上下文 使用 $ 来引用当前模板根级的上下文 使用 $var 来访问创建的变量 模板中支持的 go 语言符号 ~~~ {{"string"}} // 一般 string {{`raw string`}} // 原始 string {{'c'}} // byte {{print nil}} // nil 也被支持 ~~~ 基本语法代码: ~~~ |-- admin | |--controllers | `-- user.go ~~~ ~~~ package admin import ( "github.com/astaxie/beego" ) type UserController struct { beego.Controller } func (this *UserController) Index() { // if … else … end this.Data["if"] = true this.Data["else"] = "else" this.Data["elseif"] = false /* range … end 支持的类型为 array, slice, map, channel range 循环内部的 . 改变为以上类型的子元素 对应的值长度为 0 时,range 不会执行,. 不会改变 */ pages := []struct { Num int }{{10}, {20}, {30}} this.Data["Total"] = 100 this.Data["Pages"] = pages // with … end type stu struct { Name string Age int } this.Data["struct"] = &stu{Name: "Murphy", Age: 28} // define 可以用来定义自模板,可用于模块定义和模板嵌套 this.Data["define"] = "this is define" this.TplName = "admin/user/index.html" } ~~~ ~~~ |-- views | |--admin | |--user | `-- index.html ~~~ ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> this is view admin/user/index.html <br/> 语法 if … else … end :<br/> {{if .if}} this if = true<br/> {{end}} 嵌套的循环 :<br/> {{if .if}} this if = true<br/> {{if .else}} this else = "else" {{end}} {{end}}<br/> else if 使用 : {{if .elseif}} this elseif = false {{else if .if}} this if = true<br/> {{else}} else is ??? {{end}}<br/> 语法 range … end :<br/> 使用 .Num 输出子元素的 Num 属性,使用 $. 引用模板中的根级上下文 :<br/> {{range .Pages}} {{.Num}} of {{$.Total}}<br/> {{end}} 使用创建的变量,在这里和 go 中的 range 用法是相同的 :<br/> {{range $index, $elem := .Pages}} {{$index}} - {{$elem.Num}} - {{.Num}} of {{$.Total}}<br/> {{end}} range else :<br/> {{range .Page}} {{.Num}}... {{else}} 当 .Pages 为空 或者 长度为 0 时会执行这里 {{end}}<br/> 语法 with … end :<br/> with 用于重定向 :<br/> {{with .struct}} ---{{.Name}} {{end}}<br/> with 对变量赋值操作 : <br/> {{with $value := "My name is %s"}} {{printf . "Murphy"}} {{end}}<br/> with else : {{with .struct1}} this is with else true {{else}} this is with else false {{/* 当 pipeline 为空时会执行这里 */}} {{end}}<br/> 语法 define 用来定义自模板,可用于模块定义和模板嵌套 :<br/> {{define "loop"}} define:<li>{{.define}}</li> {{end}} 语法 template 调用模板 :<br/> <ul> template:{{template "loop" .}} </ul> <!-- 模板注释:允许多行文本注释,不允许嵌套 --> {{/* comment content support new line */}} </body> </html> ~~~ 浏览器访问: http://127.0.0.1:8080/admin/user/index 浏览器返回: ~~~ this is view admin/user/index.html 语法 if … else … end : this if = true 嵌套的循环 : this if = true this else = "else" else if 使用 : this if = true 语法 range … end : 使用 .Num 输出子元素的 Num 属性,使用 $. 引用模板中的根级上下文 : 10 of 100 20 of 100 30 of 100 使用创建的变量,在这里和 go 中的 range 用法是相同的 : 0 - 10 - 10 of 100 1 - 20 - 20 of 100 2 - 30 - 30 of 100 range else : 当 .Pages 为空 或者 长度为 0 时会执行这里 语法 with … end : with 用于重定向 : ---Murphy with 对变量赋值操作 : My name is Murphy with else : this is with else false 语法 define 用来定义自模板,可用于模块定义和模板嵌套 : 语法 template 调用模板 : template: define: this is define ~~~