ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
基本函数 变量可以使用符号 | 在函数间传递 {{.Con | markdown | addlinks}} {{.Name | printf "%s"}} 使用括号 {{printf "nums is %s %d" (printf "%d %d" 1 2) 3}} 代码实现: ~~~ |-- admin | |--controllers | `-- user.go ~~~ ~~~ package admin import ( "fmt" "net/url" "github.com/astaxie/beego" ) type UserController struct { beego.Controller } func (this *UserController) Index() { // 函数 and , 函数 or this.Data["x"] = "x" this.Data["y"] = "y" this.Data["z"] = "z" // 函数 call this.Data["dump"] = fmt.Println this.Data["arg1"] = 1 this.Data["arg2"] = 2 // 函数 index this.Data["index"] = map[string]string{"this": "is", "function": "index"} // 函数 len this.Data["len"] = [10]int{} // 函数 not this.Data["not1"] = true this.Data["not2"] = false this.Data["not3"] = "not" this.Data["not4"] = "true" // 函数 urlquery urlencode, _ := url.ParseQuery("http://beego.me") this.Data["urlencode"] = urlencode.Encode() 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/> 函数 and :<br/> <!-- and 会逐一判断每个参数,将返回第一个为空的参数,否则就返回最后一个非空参数 --> { {and .a .b .c} } 返回空 {{and .a .b .c}}<br/> { {and .x .y .z} } 返回最后一个非空值 {{and .x .y .z}}<br/> 函数 or :<br/> <!-- or 会逐一判断每个参数,将返回第一个非空的参数,否则就返回最后一个参数 --> { {and .a .b .c} } 返回最后一个参数 {{and .a .b .c}}<br/> { {and .x .y .z} } 返回第一个非空的参数 {{and .x .y .z}}<br/> 函数 call :<br/> <!-- call 可以调用函数,并传入参数 调用的函数需要返回 1 个值 或者 2 个值,返回两个值时,第二个值用于返回 error 类型的错误。返回的错误不等于 nil 时,执行将终止。 --> {{call .dump .arg .arg}}<br/> 函数 index :<br/> <!-- index 支持 map, slice, array, string,读取指定类型对应下标的值 --> { {index .index "function"} } 下标对应的值是: {{index .index "function"}}<br/> 函数 len :<br/> <!-- 返回对应类型的长度,支持类型:map, slice, array, string, chan --> {{printf "The len length is %d" (.len|len)}}<br/> 函数 not :<br/> <!-- not 返回输入参数的否定值,if true then false else true --> not1 : {{not .not1}}<br/> not2 : {{not .not2}}<br/> not3 : {{not .not3}}<br/> not4 : {{not .not4}}<br/> 函数 print 对应 fmt.Sprint<br/> 函数 printf 对应 fmt.Sprintf<br/> 函数 println 对应 fmt.Sprintln<br/> {{printf "x => %s , y => %s , z => %v\n" .x .y .z}} 函数 urlquery :<br/> {{urlquery "http://beego.me"}}<br/> url.Encode() => {{.urlencode}}<br/> 函数 eq / ne / lt / le / gt / ge 这类函数一般配合在 if 中使用 </body> </html> ~~~