# Template Functions

Beego supports custom template functions but it must be set as below before web.Run():

	func hello(in string)(out string){
		out = in + "world"
		return
	}
	
	web.AddFuncMap("hi",hello)
1
2
3
4
5
6

Then you can use these functions in template:

	{{.Content | hi}}
1

Here are Beego's built-in template functions:

  • dateformat: Format time, return string.
    {{dateformat .Time "2006-01-02T15:04:05Z07:00"}}
    
    1
  • date: This is similar to date function in PHP. It can easily return time by string
    {{date .T "Y-m-d H:i:s"}}
    
    1
  • compare: Compare two objects. If they are the same return true otherwise return false.
    {{compare .A .B}}
    
    1
  • substr: Return sub string. supports UTF-8 string.
    {{substr .Str 0 30}}
    
    1
  • html2str: Parse html to string by removing tags like script and css and return text.
    {{html2str .Htmlinfo}}
    
    1
  • str2html: Parse string to HTML, no escaping.
    {{str2html .Strhtml}}
    
    1
  • htmlquote: Escaping html.
    {{htmlquote .quote}}
    
    1
  • htmlunquote: Unescaping to html.
    {{htmlunquote .unquote}}
    
    1
  • renderform: Generate form from StructTag.
    {{&struct | renderform}}
    
    1
  • assets_js: Create a <script> tag from js src.
    {{assets_js src}}
    
    1
  • assets_css: Create a <link> tag from css src.
    {{assets_css src}}
    
    1
  • config: Get the value of AppConfig and the type must be String, Bool, Int, Int64, Float, or DIY
    {{config configType configKey defaultValue}}
    
    1
  • map_get: Get value of map by key
                // In controller
                Data["m"] = map[string]interface{} {
                    "a": 1,
                    "1": map[string]float64{
                        "c": 4,
                    },
                }
      
                // In view
                {{ map_get m "a" }} // return 1
                {{ map_get m 1 "c" }} // return 4
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
  • urlfor: Get the URL of a controller method
      {{urlfor "TestController.List"}}
    
    1