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
+16.3k Golang :Trim white spaces from a string
+9.4k Golang : Timeout example
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+10.5k Swift : Convert (cast) String to Integer
+19.6k Golang : Close channel after ticker stopped example
+19.6k Golang : Set or Add HTTP Request Headers
+23.5k Golang : Read a file into an array or slice example
+8.5k PHP : How to parse ElasticSearch JSON ?
+4.3k Javascript : How to show different content with noscript?
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+19.3k Golang : Get host name or domain name from IP address