Go Template Walkthrough Guide
Concepts & Syntax
They’re delimited by {{ }}
. Other, other non-delimited parts are left untouched.
Data evaluations
To obtain data from a struct, you can use the {{ .FieldName}}
action, which will replace it with the FieldName
value of the given struct, on parse time. The struct is given to the Execute
function, which we’ll cover later.
There’s also the {{.}}
action that you can use to refer to a value of non-struct types.
Conditions
if
,else if
,end
structure is supported. For example:
Loops
Using the range
action you can loop through a slice. A range
actions is defined using the {{range .Member}} ... {{end}}
template.
If your slice is a non-struct type, you can refer to the value using the {{ . }}
action. In case of structs, you can refer to the value using the {{ .Member }}
action, as already explained.
Functions, Pipelines and Variables
Actions have several built-in functions that are used along with pipelines to additionally parse output. Pipelines are annotated with |
and the default behavior is sending data from the left side to the function to the right side.
Functions are used to escape the action’s result. There’re several functions available by default such as, html
which returns HTML escaped output, safe against code injection or js
which returns JavaScript escaped output.
Using the with
action, you can define variables that are available in that with
block: {{ with $x := <^>result-of-some-action<^> }} {{ $x }} {{ end }}
.
Parsing Templates
The three most important and most frequently used functions are:
New
— allocates new, undefined template,Parse
— parses given template string and return parsed template,Execute
— applies parsed template to the data structure and writes result to the given writer.
Let's run through a sample:
We can reuse the same template t
, without needing to create or parse it again by providing the struct you want to use to the Execute
function again:
As you can see, templates provide a powerful way to customize textual output. Beside manipulating textual output, you can also manipulate HTML output using the html/template
package.
Verifying Templates
template
packages provide the Must
functions, used to verify that a template is valid during parsing. The Must
function provides the same result as if we manually checked for the error, like in the previous example.
This approach saves you typing, but if you encounter an error, your application will panic. For advanced error handling, it’s easier to use above solution instead of Must
function.
The Must
function takes a template and error as arguments. It’s common to provide New
function as an argument to it:
Implementing Templates
HTML templating is also common.
Note: In line 67 above, if we use an ad-hoc template name say
html-template
, we would get error:panic: template: "html-template" is an incomplete or empty template
So the template name MUST be the FIRST FILE NAME if there are many template files.
How if we have multiple template files?
We need to define
the template names:
But we get only first file todo.tpl
parsed and printed. Why? The solution here is to refer/include those we want in the first template:
Please note this line: {{ template "todo2" . }}
References
Last updated