<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">(window.webpackJsonp=window.webpackJsonp||[]).push([[96],{408:function(t,e,a){"use strict";a.r(e);var s=a(14),r=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"template-functions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#template-functions"}},[t._v("#")]),t._v(" Template Functions")]),t._v(" "),e("p",[t._v("Beego supports custom template functions, but it must be set as below before "),e("code",[t._v("web.Run()")]),t._v(":")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('\tfunc hello(in string)(out string){\n\t\tout = in + "world"\n\t\treturn\n\t}\n\t\n\tweb.AddFuncMap("hi",hello)\n')])])]),e("p",[t._v("Then you can use these functions in template:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("\t{{.Content | hi}}\n")])])]),e("p",[t._v("Here are Beego's built-in template functions:")]),t._v(" "),e("ul",[e("li",[t._v("dateformat: Format time, return string."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('{{dateformat .Time "2006-01-02T15:04:05Z07:00"}}\n')])])])]),t._v(" "),e("li",[t._v("date: This is similar to date function in PHP. It can easily return time by string."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('{{date .T "Y-m-d H:i:s"}}\n')])])])]),t._v(" "),e("li",[t._v("compare: Compare two objects. If they are the same return true, otherwise return false."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{compare .A .B}}\n")])])])]),t._v(" "),e("li",[t._v("substr: Return sub string, supports UTF-8 string."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{substr .Str 0 30}}\n")])])])]),t._v(" "),e("li",[t._v("html2str: Parse html to string by removing tags like script and css and return text."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{html2str .Htmlinfo}}\n")])])])]),t._v(" "),e("li",[t._v("str2html: Parse string to HTML, no escaping."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{str2html .Strhtml}}\n")])])])]),t._v(" "),e("li",[t._v("htmlquote: Escaping html."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{htmlquote .quote}}\n")])])])]),t._v(" "),e("li",[t._v("htmlunquote: Unescaping to html."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{htmlunquote .unquote}}\n")])])])]),t._v(" "),e("li",[t._v("renderform: Generate form from StructTag."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{&amp;struct | renderform}}\n")])])])]),t._v(" "),e("li",[t._v("assets_js: Create a "),e("code",[t._v("&lt;script&gt;")]),t._v(" tag from js src."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{assets_js src}}\n")])])])]),t._v(" "),e("li",[t._v("assets_css: Create a "),e("code",[t._v("&lt;link&gt;")]),t._v(" tag from css src."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{assets_css src}}\n")])])])]),t._v(" "),e("li",[t._v("config: Get the value of AppConfig and the type must be String, Bool, Int, Int64, Float, or DIY."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{config configType configKey defaultValue}}\n")])])])]),t._v(" "),e("li",[t._v("map_get: Get value of "),e("code",[t._v("map")]),t._v(" by key."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('            // In controller\n            Data["m"] = map[string]interface{} {\n                "a": 1,\n                "1": map[string]float64{\n                    "c": 4,\n                },\n            }\n  \n            // In view\n            {{ map_get m "a" }} // return 1\n            {{ map_get m 1 "c" }} // return 4\n')])])])]),t._v(" "),e("li",[t._v("urlfor: Get the URL of a controller method."),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('  {{urlfor "TestController.List"}}\n')])])])])])])}),[],!1,null,null,null);e.default=r.exports}}]);</pre></body></html>