Golang html/template.Template.AddParseTree function example

package html/template

AddParseTree creates a new template with the name(1st parameter) and parse tree(2nd parameter and associates it with t.

It returns an error if t has already been executed.

Golang html/template.Template.AddParseTree function usage example

 err := filepath.Walk(templateDir, func(path string, info os.FileInfo, err error) error {
 if err != nil {
 return err
 }
 if info.IsDir() {
 return nil
 }
 rel, err := filepath.Rel(templateDir, path)
 if err != nil {
 return err
 }
 t := template.Must(template.ParseFiles(path))
 log.Printf("  %v\n", rel)
 templates = template.Must(templates.AddParseTree(rel, t.Tree)) // <--- here
 return nil
  })
  if err != nil {
 log.Fatal("Unable to load templates.")
  }

References :

https://github.com/bklimt/weather/blob/master/cmd/weather/templates.go

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

Advertisement