首页
网站导航
关于
Search
1
解决Typecho Joe主题访问fastly.jsdelivr.net速度慢的方案 | 快速优化技巧
2,191 阅读
2
解决WSL2内存不释放问题的最佳指南
1,406 阅读
3
如何在 Typecho Joe 主题的文章中增加目录
1,171 阅读
4
GO语言环境的搭建教程 - 完全指南
1,005 阅读
5
如何解决Win11电脑桌面上方显示横线问题 | 窗口11教程
1,005 阅读
默认分类
编程语言
GO语言
PHP
Node
javascript
html
rust
java
Css
Python
资源分享
chrome插件
阅读思考
运维架构
redis
Nginx
linux
memcached
mongodb
mysql
windows
docker
k8s
Mq
apache
CI
Git
swoole
elk
系统设计
thinkPhp
beego
登录
Search
标签搜索
重要
go基础
git 命令
go包
phpstorm
sublime
thinkphp6
mysql问题
软件分享
redis命令
php基础
thinkphp3.2
php第三扩展包
小蚯蚓博客
累计撰写
333
篇文章
累计收到
48
条评论
首页
栏目
默认分类
编程语言
GO语言
PHP
Node
javascript
html
rust
java
Css
Python
资源分享
chrome插件
阅读思考
运维架构
redis
Nginx
linux
memcached
mongodb
mysql
windows
docker
k8s
Mq
apache
CI
Git
swoole
elk
系统设计
thinkPhp
beego
页面
网站导航
关于
搜索到
1
篇与
的结果
2022-06-08
深入理解Beego模板语法 - 教程与使用指南
一、基本语法 go统一使用{{和}}符号作为左右标签,没有其它的标签符号。使用.来访问当前位置的上下文,使用$来引用当前模板根级的上下文,使用$var来访问创建的变量。 1.模板中支持的go语言符号 {{"string"}} // 一般 string {{`raw string`}} // 原始 string {{'c'}} // byte {{print nil}} // nil 也被支持 2.模板中的pipeline(管道) 可以是上下文的变量输出,也可以是函数通过管道传递的返回值。 {{. | FuncA | FuncB | FuncC}} 当pipeline的值等于: false或0 nil的指针或interface 长度为0的array、slice、map、string 那么这个pipeline被认为是空。 3.逻辑处理 (1)if...else...end {{if pipeline}}{{end}} if判断时,pipeline为空时,相当于判断为False this.Data["IsLogin"] = true this.Data["IsHome"] = true this.Data["IsAbout"] = true 支持嵌套的循环 {{if .IsHome}} {{else}} {{if .IsAbout}}{{end}} {{end}} 也可以使用else if进行 {{if .IsHome}} {{else if .IsAbout}} {{else}} {{end}} (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}} (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}} (4)define define 可以用来定义自模板,可用于模块定义和模板嵌套 {{define "loop"}} <li>{{.Name}}</li> {{end}} 使用 template 调用模板 <ul> {{range .Items}} {{template "loop" .}} {{end}} </ul> (5)template {{template "模板名" pipeline}} 将对应的上下文 pipeline 传给模板,才可以在模板中调用 3.beego中支持直接载入文件模板 {{template "path/to/head.html" .}} Beego 会依据你设置的模板路径读取 head.html 在模板中可以接着载入其他模板,对于模板的分模块处理很有用处 4.注释 允许多行文本注释,不允许嵌套 {{/* comment contentsupport new line */}} 二、基本函数 变量可以使用符号|在函数间传递 {{.Con | markdown | addlinks}} {{.Name | printf "%s"}} 使用括号 {{printf "nums is %s %d" (printf "%d %d" 1 2) 3}} (1)and {{and .X .Y .Z}} and 会逐一判断每个参数,将返回第一个为空的参数,否则就返回最后一个非空参数 (2)call {{call .Field.Func .Arg1 .Arg2}} call 可以调用函数,并传入参数 调用的函数需要返回 1 个值 或者 2 个值,返回两个值时,第二个值用于返回 error 类型的错误。返回的错误不等于 nil 时,执行将终止。 (3)index index 支持 map, slice, array, string,读取指定类型对应下标的值 this.Data["Maps"] = map[string]string{"name": "Beego"} {{index .Maps "name"}} (4)len {{printf "The content length is %d" (.Content|len)}} 返回对应类型的长度,支持类型:map, slice, array, string, chan (5)not not 返回输入参数的否定值,if true then false else true (6)or {{or .X .Y .Z}} or 会逐一判断每个参数,将返回第一个非空的参数,否则就返回最后一个参数 (7)print 对应 fmt.Sprint (8)printf 对应fmt.Sprintf (9)pfintln 对应fmt.Sprintf (10)urlquery {{urlquery "http://beego.me"}} 将返回 http%3A%2F%2Fbeego.me (11)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}} {{if lt 100 200}}{{end}}
2022年06月08日
272 阅读
0 评论
0 点赞