I have a Golang template, defined like this
{{- define "test" -}}
{{- printf "%s" .Name | trunc 24 -}}
{{- end -}}
Then I use it in one of my files:
{{ template "test" . }}
What does the dot mean after "test"? Golang template docs say:
{{template "name" pipeline}}
The template with the specified name is executed with dot set
to the value of the pipeline.
But I am not sure what pipeline is. Reading documentation gave no results, could anyone explain once again?
Also, why do we have to start values beginning with dot? E.g. {{ - printf "%s" .Name | trunc 24 -}}
. Is it also a kind of pipeline?
Thank you in advance!
There are 2
template
packages,text/template
andhtml/template
.They have the same interface, but the
html/template
package is for generating HTML output safe against code injection, and should be used instead oftext/template
whenever the output is HTML.Since they have the same interface but the
html/template
provides some extra functionality (contextual escaping of the inserted data), the basics and principles are only documented attext/html
, and the documentation ofhtml/template
mostly focuses on detailing the extra.That being said, "pipeline" belongs to the basics. It is documented in
text/template
, section Pipelines:"Arguments" and "pipelines" are evaluations of data.
The "dot"
.
is basically a cursor, pointing to somewhere in the data structure you pass when executing the template. The starting value of the dot is the value you pass, but this dot is modified by many actions, such as{{range}}
or{{with}}
.So when you write
.Name
, that means that the value where dot is pointing currently, you want to refer to its field or method or key calledName
. For example if you pass astruct
, at the beginning of your template.Name
will denote the struct fieldName
if it exists, or its method namedName()
.When you invoke / include another template, you have the possibility to tell what value you what to pass to its execution. When you write
{{template "something" .}}
, that means you want to pass the value currently pointed by dot to the template execution. If you want to pass only theName
field of the struct pointed by the dot, you may do it like{{template "something" .Name}}
.The value you pass as the pipeline in
{{template}}
will become the dot inside the invoked other template.So as your template is being processed / rendered, the dot may be changed and point "only" to a part of the value originally passed to your template execution. Often it's handy or required to still reach the original value and not just the cursor. For this the template package provides the
$
:So even if you're inside a
{{range}}
for example (which sets the dot to the successive elements of the array / slice / map you're ranging over), you can still reach out and refer to any other parts of the value passed to the template execution.So for example if you're ranging over a slice of books like
{{range .Books}}
, and if you need theName
field of the originally passed struct, you may do it inside{{range}}
like this: