In the html/template
(and text/template
) packages, template.New
has the following signature:
func New(name string) *Template
What exactly is the name
used for? I've scanned the docs (and a bit of source), but to no avail. I just instantiate all of my templates with an empty string and it doesn't seem to make a difference. Why should I bother with a name?
Even for naming templates, the two seem equivalent:
template.Must(template.New("").Parse(`{{ define "body" }}Body{{ end }}`))
template.Must(template.New("body").Parse(`Body`))
The name of the template–unsurprisingly–is to name the template.
What is it good for? As long as you don't want to refer to the template, it doesn't really matter. But if you want to refer to it, then yes, you refer to it by its name.
When would you want to refer to it? When you want to include a template in another e.g. using the
{{template}}
action, or when you want to execute a specific template usingTemplate.ExecuteTemplate()
.So far so good, but there's still a missing key point. This is not unambiguous / trivial: a
template.Template
value is "the representation of a parsed template". But the wording here is a little "imperfect". Atemplate.Template
value may be (and usually is) a collection of multiple, associated templates.template.Template
has an unexported field:This
tmpl
field holds all other associated templates, templates that are visible to the template, and which can be referred to–yes–by their names.When you parse multiple templates at once, using
Template.ParseFiles()
orTemplate.ParseGlob()
, then the templates will be named by the file names, and they will be associated automatically (the above mentioned functions return a singletemplate.Template
value, which holds all the parsed templates, associated). Doc ofTemplate.ParseFiles()
is clear on this:The template name can come from multiple places:
{{define "somename"}}
or{{block "somename"}}
actions),template.New()
(function) orTemplate.New()
(method).Let's see some examples:
Output (try it on the Go Playground):
If you now go ahead, and change the first 2 lines to this:
Then what happens here is that we assigned a new
template.Template
value tot
, which was the result of parsingt2src
, so that will be the default, but still both templates can be "reached" from it as they are associated. The output changes to this (try it on the Go Playground):Calling
template.New()
(function) creates a new template, associated to none. When callingTemplate.New()
(method), the returned template will be associated with (all) the template(s) the method is called on.Now let's see some examples regarding "embedded" templates.
Output (try it on the Go Playground):
It should be obvious now what the role of the template name is, and where it comes from.
It is used to render associated templates.
For instance:
Play Example
Output:
tmpl.ExecuteTemplate(os.Stdout, "base", nil)
will render the template using the "base" templatetmpl.ExecuteTemplate(os.Stdout, "baz", nil)
will render the template using the "baz" template