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
+8.3k Golang : Oanda bot with Telegram and RSI example
+14k Golang : concatenate(combine) strings
+11.7k Golang : Gorilla web tool kit secure cookie example
+35.5k Golang : Smarter Error Handling with strings.Contains()
+24.5k Golang : Change file read or write permission example
+8.3k Golang : Auto-generate reply email with text/template package
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+16.3k Golang : Loop each day of the current month example
+30.4k Golang : Generate random string
+5.2k Golang : Customize scanner.Scanner to treat dash as part of identifier
+7.7k Golang : Test if an input is an Armstrong number example
+31.9k Golang : Convert an image file to []byte