summaryrefslogtreecommitdiff
path: root/docs/content/syntax.md
blob: 5e5389e89597c17fc27fd3c0f7dbd6090e0deb29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
---
title: Syntax
weight: 13
menu: main
---

Gomplate uses the syntax understood by the Go language's [`text/template`][]
package. This page documents some of that syntax, but see [the language docs][`text/template`]
for full details.

## The basics

Templates are just regular text, with special actions delimited by `{{` and `}}` markers. Consider the following template:

```
Hello, {{ print "World" }}!
```

If you render this template, it will produce the following output:

```
Hello, World!
```

This is obviously a contrived example, and you would likely never see this in
_real life_, but this conveys the basics, which is that _actions_ are  delimited
by `{{` and `}}`, and are replaced with their output (if any) when the template
is rendered.

## Multi-line templates

By default, every line containing an action will render a newline. For example, the action block below:

```
{{ range coll.Slice "Foo" "bar" "baz" }}
Hello, {{ . }}!
{{ end }}
```

will produce the output below:

```

Hello, Foo!

Hello,  bar!

Hello,  baz!

```

This might not be desirable.

You can use [Golang template syntax](https://pkg.go.dev/text/template/#hdr-Text_and_spaces) to fix this. Leading newlines (i.e. newlines that come before the action) can be suppressed by placing a minus sign in front of the first set of delimiters (`{{`). Putting the minus sign behind the trailing set of delimiters (`}}`) will suppress the newline _after_ the action. You can do both to suppress newlines entirely on that line.

Placing the minus sign within the context (i.e. inside of `{{.}}`) has no effect.

Here are a few examples.

### Suppressing leading newlines

```
{{- range coll.Slice "Foo" "bar" "baz" }}
Hello, {{ . }}!
{{- end }}
```

will produce this:

```

Hello, Foo!
Hello,  bar!
Hello,  baz!
```

### Suppressing trailing newlines

This code:

```
{{ range coll.Slice "Foo" "bar" "baz" -}}
Hello, {{ . }}!
{{ end -}}
```

yields this:

```
Hello, Foo!
Hello,  bar!
Hello,  baz!
```

### Suppressing newlines altogether

This code:

```
{{- range coll.Slice "Foo" "bar" "baz" -}}
Hello, {{ . }}!
{{- end -}}
```

Produces:

```
Hello, Foo!Hello,  bar!Hello,  baz!
```

## Variables

The result of an action can be assigned to a _variable_, which is denoted by a
leading `$` character, followed by an alphanumeric string. For example:

```
{{ $w := "world" }}
Hello, {{ print $w }}!
Goodbye, {{ print $w }}.
```

this will render as:

```
Hello, world!
Goodbye, world.
```

Variables are declared with `:=`, and can be redefined with `=`:

```
{{ $w := "hello" }}
{{ $w = "goodbye" }}
```

### Variable scope

A variable's scope extends to the `end` action of the control structure (`if`,
`with`, or `range`) in which it is declared, or to the end of the template if
there is no such control structure.

In other words, if a variable is initialized inside an `if` or `else` block,
it cannot be referenced outside that block.

This template will error with `undefined variable "$w"` since `$w` is only
declared within `if`/`else` blocks:

```
{{ if 1 }}
{{ $w := "world" }}
{{ else }}
{{ $w := "earth" }}
{{ end }}

Hello, {{ print $w }}!
Goodbye, {{ print $w }}.
```

One way to approach this is to declare the variable first to an empty value:

```
{{ $w := "" }}
{{ if 1 }}
{{ $w = "world" }}
{{ else }}
{{ $w = "earth" }}
{{ end -}}

Hello, {{ print $w }}!
Goodbye, {{ print $w }}.
```

## Indexing arrays and maps

Occasionally, multi-dimensional data such as arrays (lists, slices) and maps
(dictionaries) are used in templates, sometimes through the use of
[data sources][]. Accessing values within these data can be done in a few ways
which bear clarifying.

### Arrays

Arrays are always numerically-indexed, and individual values can be accessed with the `index` built-in function:

```
{{ index $array 0 }}
```

To visit each value, you can loop through an array with `range`:

```
{{ range $array }}
do something with {{ . }}...
{{ end }}
```

If you need to keep track of the index number, you can declare two variables, separated by a comma:

```
{{ range $index, $element := $array }}
do something with {{ $element }}, which is number {{ $index }}
{{ end }}
```

### Maps

For maps, accessing values can be done with the `.` operator. Given a map `$map`
with a key `foo`, you could access it like:

```
{{ $map.foo }}
```

However, this kind of access is limited to keys which are strings and contain
only characters in the set (`a`-`z`,`A`-`Z`,`_`,`1`-`9`), and which do not begin
with a number. If the key doesn't conform to these rules, you can use the `index`
built-in function instead:

```
{{ index $map "foo-bar" }}
```

`index` also supports nested keys and can be combined with other functions as such:

```
{{ index $map "foo" (env.Getenv "BAR") "baz" ... }}
``` 

**Note:** _while `index` can be used to access awkwardly-named values in maps,
it behaves differently than the `.` operator. If the key doesn't exist, `index`
will simply not return a value, while `.` will error._

And, similar to arrays, you can loop through a map with the `range`:

```
{{ range $map }}
The value is {{ . }}
{{ end }}
```

Or if you need keys as well:

```
{{ range $key, $value := $map }}
{{ $key }}'s value is: {{ $value }}
{{ end }}
```

## Functions

Almost all of gomplate's utility is provided as _functions._ These are key
words (like `print` in the previous examples) that perform some action.

See the [functions documentation](/functions/) for more information.

## The Context

Go templates are always executed with a _context_. You can reference the context
with the `.` (period) character, and you can set the context in a block with the
`with` action. Like so:

```
$ gomplate -i '{{ with "foo" }}The context is {{ . }}{{ end }}'
The context is foo
```

Templates rendered by gomplate always have a _default_ context. You can populate
the default context from data sources with the [`--context`/`c`](../usage/#--context-c)
flag. The special context item [`.Env`](#env) is available for referencing the
system's environment variables.

_Note:_ The initial context (`.`) is always available as the variable `$`,
so the initial context is always available, even when shadowed with `range`
or `with` blocks:

```
$ echo '{"bar":"baz"}' | gomplate -c .=stdin:///in.json -i 'context is: {{ . }}
{{ with "foo" }}now context is {{ . }}
but the original context is still {{ $ }}
{{ end }}'
context is: map[bar:baz]
now context is foo
but the original context is still map[bar:baz]
```

## Nested templates

Gomplate supports nested templates, using Go's `template` action. These can be
defined in-line with the `define` action, or external data can be used with the
[`--template`/`-t`](../usage/#--template-t) flag.

Note that nested templates do _not_ have access to gomplate's default
[context](#the-context) (though it can be explicitly provided to the `template`
action).

### In-line templates

To define a nested template in-line, you can use the `define` action.

```
{{ define "T1" -}}
Hello {{ . }}!
{{- end -}}

{{ template "T1" "World" }}
{{ template "T1" }}
{{ template "T1" "everybody" }}
```

This renders as:

```
Hello World!
Hello <no value>!
Hello everybody!
```

### External templates

To define a nested template from an external source such as a file, use the
[`--template`/`-t`](../usage/#--template-t) flag.

_hello.t:_
```
Hello {{ . }}!
```

```
$ gomplate -t hello=hello.t -i '{{ template "hello" "World" }} {{ template "hello" .Env.USER }}'
Hello World! Hello hairyhenderson!
```

## `.Env`

You can easily access environment variables with `.Env`, but there's a catch:
if you try to reference an environment variable that doesn't exist, parsing
will fail and `gomplate` will exit with an error condition.

For example:

```console
$ gomplate -i 'the user is {{ .Env.USER }}'
the user is hairyhenderson
$ gomplate -i 'this will fail: {{ .Env.BOGUS }}'
this will fail: template: <arg>:1:23: executing "<arg>" at <.Env.BOGUS>: map has no entry for key "BOGUS"
```

Sometimes, this behaviour is desired; if the output is unusable without certain
strings, this is a sure way to know that variables are missing!

If you want different behaviour, try [`getenv`](../functions/env/#envgetenv).

[`text/template`]: https://pkg.go.dev/text/template/
[`base64.Encode`]: ../functions/base64#base64-encode
[data sources]: ../datasources/