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
+13.8k Golang : Gin framework accept query string by post request example
+9.7k Golang : Sort and reverse sort a slice of floats
+11.7k Golang : Secure file deletion with wipe example
+6.3k Golang : Calculate US Dollar Index (DXY)
+6.9k Nginx : Password protect a directory/folder
+9.3k Golang : How to get garbage collection data?
+9.2k Golang : Write multiple lines or divide string into multiple lines
+12.5k Golang : "https://" not allowed in import path
+13.5k Golang : How to get year, month and day?
+6.8k Golang : Calculate pivot points for a cross
+6.8k Swift : substringWithRange() function example
+22.6k Generate checksum for a file in Go