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
+7.8k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+19.8k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+5k Linux : How to set root password in Linux Mint
+9.9k Golang : Channels and buffered channels examples
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+4.6k Chrome : How to block socketloop.com links in Google SERP?
+26.5k Golang : Encrypt and decrypt data with AES crypto
+21.6k SSL : How to check if current certificate is sha1 or sha2
+23.1k Golang : Randomly pick an item from a slice/array example
+13.4k Golang : Increment string example
+40.3k Golang : Convert to io.ReadSeeker type