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
Advertisement
Something interesting
Tutorials
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+14.2k Golang : syscall.Socket example
+13.6k Golang : reCAPTCHA example
+9.7k Golang : interface - when and where to use examples
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+9.7k Golang : Populate slice with sequential integers example
+10.1k Golang : How to tokenize source code with text/scanner package?
+38.1k Golang : Read a text file and replace certain words
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+12k Golang : Convert a rune to unicode style string \u
+9.5k Golang : Changing a RGBA image number of channels with OpenCV