Golang html/template.FuncMap type examples
package html/template
FuncMap is the type of the map defining the mapping from names to functions. Each function must have either a single return value, or two return values of which the second has type error. In that case, if the second (error) argument evaluates to non-nil during execution, execution terminates and Execute returns that error. FuncMap has the same base type as FuncMap in "text/template", copied here so clients need not import "text/template".
Golang html/template.FuncMap type usage examples
Example 1:
func init() {
...
beegoTplFuncMap = make(template.FuncMap)
...
beegoTplFuncMap["dateformat"] = DateFormat
beegoTplFuncMap["date"] = Date
beegoTplFuncMap["compare"] = Compare
beegoTplFuncMap["substr"] = Substr
beegoTplFuncMap["html2str"] = Html2str
beegoTplFuncMap["str2html"] = Str2html
beegoTplFuncMap["htmlquote"] = Htmlquote
beegoTplFuncMap["htmlunquote"] = Htmlunquote
beegoTplFuncMap["renderform"] = RenderForm
beegoTplFuncMap["assets_js"] = AssetsJs
beegoTplFuncMap["assets_css"] = AssetsCss
beegoTplFuncMap["config"] = Config
// go1.2 added template funcs
// Comparisons
beegoTplFuncMap["eq"] = eq // ==
beegoTplFuncMap["ge"] = ge // >=
beegoTplFuncMap["gt"] = gt // >
beegoTplFuncMap["le"] = le // <=
beegoTplFuncMap["lt"] = lt // <
beegoTplFuncMap["ne"] = ne // !=
beegoTplFuncMap["urlfor"] = UrlFor // !=
}
Example 2:
// LoadTemplates pre-loads templates from the configured template directory
func (h *HTTP) LoadTemplates(name string) (t *template.Template, err error) {
t = template.New("t")
t.Funcs(template.FuncMap{
"add": add,
"sizeof": readableSize,
"version": version,
})
t, err = t.ParseFiles(
fmt.Sprintf("%s/base.html", GetConfig().Templates),
fmt.Sprintf("%s/%s.html", GetConfig().Templates, name))
if err != nil {
panic(err)
}
return t, err
}
References :
https://github.com/astaxie/beego/blob/master/template.go
Advertisement
Something interesting
Tutorials
+6.5k Golang : Reverse by word
+9.7k Golang : Convert octal value to string to deal with leading zero problem
+15.6k Golang : Read large file with bufio.Scanner cause token too long error
+5.9k Golang : Calculate US Dollar Index (DXY)
+7.1k Golang : Individual and total number of words counter example
+11k Golang : How to use if, eq and print properly in html template
+8.5k Golang : Executing and evaluating nested loop in html template
+9.5k Golang : ffmpeg with os/exec.Command() returns non-zero status
+6k Apt-get to install and uninstall Golang
+9.8k Golang : Use regular expression to get all upper case or lower case characters example
+13.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+17.8k Golang : Check if a directory exist or not