参考网站
https://beego.me/docs/mvc/view/tutorial.md
controller 内容
this.Data["IsLogin"] = true
this.Data["IsHome"] = true
this.Data["IsAbout"] = true
this.Data["Name"] = "victor"
this.Data["Age"] = 20
if … end
{{if .IsHome}}
如果存在 Ishome打印 1313113131
{{end}}
if … else … end
{{if .Isme}}
如果存在 Isme打印 1313113131
{{else}}
如果不存在 Isme打印 233333
{{end}}
支持嵌套的循环
{{if .Name}}
打印名称{{.Name}}
{{else}}
{{if .Age}}
继续判断年龄,存在打印 {{.Age}}
{{end}}
{{end}}
也可以使用 else if 进行
{{if .Name1}}
打印名称{{.Name}}
{{else if .Age1}}
继续判断年龄,存在打印 {{.Age}}
{{else}}
名字年龄都不存在
{{end}}
eq / ne / lt / le / gt / ge
这类函数一般配合在 if 中使用
eq: arg1 == arg2
ne: arg1 != arg2
lt: arg1 < arg2
le: arg1 <= arg2
gt: arg1 > arg2
ge: arg1 >= arg2
eq 和其他函数不一样的地方是,支持多个参数,和下面的逻辑判断相同
arg1==arg2 || arg1==arg3 || arg1==arg4 ...
与 if 一起使用
{{if eq true .Var1 .Var2 .Var3}}{{end}}
range … end
controller 配置
nums := []int{1,2,3,4,5,6,7,8,9}
c.Data["Nums"] = nums
# html文件
{{range .Nums}}
{{.}}
{{end}}
with end使用
# controller配置
type Person1 struct {
Name string
Age int
}
data := &Person1{"Ccg", 18}
c.Data["Person1"] = data
# html文件
{{with .Person1}}
{{.Name}}; {{.Age}}
{{end}}
等同于
{{.Person1.Name}}; {{.Person1.Age}}