Actions and pipelines
View as MarkdownA template is text with actions between {{ and }}. obol copies the text
as is and replaces each action with what it prints.
Hello, {{.name}}. You have {{len .unread}} new notes.
The data you pass to execute starts as dot, written .. {{.}} prints dot
itself.
Comments #
{{/* Shown to whoever edits this file, never printed. */}}
A comment opens with /* straight after the delimiter and closes with */
straight before it. It may span lines.
Trim markers #
A - and a space beside a delimiter trim the white space on that side,
newlines included:
{{23 -}} < {{- 45}}
That prints 23<45. The space matters: {{-3}} is the number −3.
Trim markers keep a template readable without leaking blank lines:
<ul>
{{- range .}}
<li>{{.}}</li>
{{- end}}
</ul>
Arguments #
| Argument | Is |
|---|---|
. |
dot, the current value |
.name |
a key or field of dot; .a.b.c chains them |
$, $x |
a variable; $ is the data passed to execute |
$x.name |
a key or field of a variable |
.greet "Hi" |
a method call; the method gets the arguments |
"text", `raw` |
a string; backquotes skip escapes and span lines |
'a' |
a character, as its code point |
42, 0x2A, 1e3 |
a number |
true, false |
a boolean |
nil |
nil, only as an argument to a function |
(pipeline) |
a pipeline in parentheses, as one value |
A key on null fails the run. A key on a missing value prints <no value>.
See Data.
Pipelines #
A pipeline is a chain of commands split by |. Each command's result becomes
the last argument of the next:
{{.title | printf "%q" | html}}
That is html (printf "%q" .title). A command is an argument, or a function
or method with arguments.
Variables #
An action can declare a variable, which lasts to the end of the control
structure that holds it, or to the end of the template:
{{$total := len .items}}
{{$total = 0}}
:= declares and = assigns. An action that declares a variable prints
nothing.
$ always holds the data passed to execute, so a range body can still
reach the top:
{{range .posts}}<a href="{{$.base}}/{{.slug}}">{{.title}}</a>{{end}}