Golang html/template.Template.Funcs function example

package html/template

Funcs adds the elements of the argument map to the template's function map. It panics if a value in the map is not a function with appropriate return type. However, it is legal to overwrite elements of the map. The return value is the template, so calls can be chained.

Golang html/template.Template.Funcs function usage example

  tmpl := template.New(path.Base(files[0])).Funcs(template.FuncMap{
 "asset":  server.templateAsset,
 "dump": templateDumpMap,
 "eq": templateEqual,
 "ne": templateNotEqual,
 "substr": templateSubstr,
  })

  // Execute template
  tmpl, err = tmpl.ParseFiles(files...)
  if err != nil {
 return err
  }
  tmplData := bytes.NewBuffer(nil)
  err = tmpl.Execute(tmplData, data)
  if err != nil {
 return err
  }
  ...

References :

https://github.com/facette/facette/blob/master/pkg/server/template.go

http://golang.org/pkg/html/template/#Template.Funcs

Advertisement