A template can actually act as a template map on its own. That's what I'm doing:
Declare a global template variable:
var t = template.New("master")
I actually do not use the master template, except as a container for other templates.
Then I load all the templates when the application starts:
func init() { _, err := t.ParseGlob("templates/*.html") if err != nil { log.Fatalln("Error loading templates:", err) } }
Then, when I want to use one of the templates, I ask for it by name:
t.ExecuteTemplate(w, "user.html", data)
andybalholm
source share