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
+32.2k Golang : Convert []string to []byte examples
+4.4k Linux/MacOSX : Search and delete files by extension
+13.7k Golang : Check if an integer is negative or positive
+15.7k Golang : Get checkbox or extract multipart form data value example
+5.9k Golang : Shuffle array of list
+7.1k Golang : Gorrila mux.Vars() function example
+19.8k Golang : Append content to a file
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+15.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+9.9k Golang : Sort and reverse sort a slice of integers
+7k Golang : constant 20013 overflows byte error message
+14k Golang : Compress and decompress file with compress/flate example