🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
模板函数 beego 支持用户定义模板函数,但是必须在 beego.Run() 调用之前,设置如下: ~~~ func hello(in string)(out string){ out = in + "world" return } beego.AddFuncMap("hi",hello) ~~~ 定义之后你就可以在模板中这样使用了: ~~~ {{.Content | hi}} ~~~ beego 内置的模板函数: ~~~ |-- admin | |--controllers | `-- user.go ~~~ ~~~ package admin import ( "time" "github.com/astaxie/beego" ) type UserController struct { beego.Controller } func (this *UserController) Index() { // 函数 dateformat , 函数 date this.Data["time"] = time.Now() // 函数 compare this.Data["A"] = "A" this.Data["B"] = "B" // 函数 substr this.Data["str"] = "hello world!" // 函数 html2str this.Data["htmlInof"] = `<? W3S?h??!??>this is function html2str <dfdjfdk>` // 函数 str2html this.Data["strHtml"] = `<h3>this is function html2str </h3>` // 函数 htmlquote this.Data["quote"] = `<h3>this is function html2str </h3>` // 函数 htmlunquote this.Data["unquote"] = `&lt;h3&gt;this&nbsp;is&nbsp;function&nbsp;html2str&nbsp;&lt;/h3&gt;` // 函数 renderform type stu struct { Name string `form:"user_name"` Age int `form:"user_age"` Class int } // <input name="user_name" type="text" value=""> // <input name="user_age" type="text" value="0"> // <input name="Class" type="text" value="0"> this.Data["struct"] = &stu{} // 函数 assets_js this.Data["js_src"] = "./public/js/test.js" // <script src="./public/js/test.js"></script> // 函数 assets_css this.Data["css_src"] = "./public/css/test.css" // <link href="./public/css/test.css" rel="stylesheet"> // 函数 map_get this.Data["map"] = map[string]interface{}{ "key1": "value1", "key2": map[string]string{"key3": "value2"}, } 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/> 函数 dateformat :<br/> <!-- 实现了时间的格式化,返回字符串 --> {{dateformat .time "2006-01-02 15:04:05"}} <br/> 函数 date :<br/> <!-- 实现了类似 PHP 的 date 函数,可以很方便的根据字符串返回时间 --> {{date .time "Y-m-d H:i:s"}}<br/> 函数 compare :<br/> <!-- 实现了比较两个对象的比较,如果相同返回 true,否者 false --> { {compare .A .B} } => {{compare .A .B}}<br/> 函数 substr :<br/> <!-- 实现了字符串的截取,支持中文截取的完美截取 --> {{substr .str 0 6}}<br/> {{substr .str 6 20}}<br/> 函数 html2str :<br/> <!-- 实现了把 html 转化为字符串,剔除一些 script、css 之类的元素,返回纯文本信息 --> {{html2str .htmlInof}}<br/> 函数 str2html :<br/> <!-- 实现了把相应的字符串当作 HTML 来输出,不转义 --> {{str2html .strHtml}}<br/> 函数 htmlquote :<br/> <!-- 实现了基本的 html 字符转义 --> {{htmlquote .quote}}<br/> 函数 htmlunquote :<br/> <!-- 实现了基本的反转移字符 --> {{htmlunquote .unquote}}<br/> 函数 renderform :<br/> <!-- 根据 StructTag 直接生成对应的表单 --> {{.struct | renderform}}<br/> 函数 assets_js :<br/> <!-- 为 js 文件生成一个 <script> 标签. --> {{assets_js .js_src}}<br/> 函数 assets_css :<br/> <!-- 为 css 文件生成一个 <link> 标签. --> {{assets_css .css_src}} 函数 config :<br/> <!-- 获取 AppConfig 的值. 可选的 configType 有 String, Bool, Int, Int64, Float, DIY --> {{config "String" "appname" "default"}}<br/> 函数 map_get :<br/> <!-- 获取 map 的值 --> {{map_get .map "key1"}}<br/> {{map_get .map "key2" "key3"}}<br/> 函数 urlfor :<br/> <!-- 获取控制器方法的 URL --> {{urlfor "UserController.Index"}} </body> </html> ~~~