summaryrefslogtreecommitdiff
path: root/docs/content/functions/_index.md
blob: adb44e2bb64ac0f09eb3b6cc7f912394e57884a5 (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
---
title: Functions
---

Almost all of gomplate's utility is provided as _functions._ These are key
words that perform some action.

For example, the [`base64.Encode`][] function will encode some input string as
a base-64 string:

```
The word is {{ base64.Encode "swordfish" }}
```
renders as:
```
The word is c3dvcmRmaXNo
```

## Built-ins

Go's [`text/template`][] language provides a number of built-in functions, 
operators, and actions that can be used in templates.

### Built-in functions

Here is a list of the built-in functions, but see [the documentation](https://golang.org/pkg/text/template/#hdr-Functions)
for full details:

- `and`, `or`, `not`: Returns boolean AND/OR/NOT of the argument(s).
- `call`: Returns the result of calling a function argument.
- `html`, `js`, `urlquery`: Safely escapes input for inclusion in HTML, JavaScript, and URL query strings.
- `index`: Returns the referenced element of an array/slice, string, or map. See also [Arrays](../syntax/#arrays) and [Maps](../syntax/#maps).
- `len`: Returns the length of the argument.
- `print`, `printf`, `println`: Aliases for Go's [`fmt.Print`](https://golang.org/pkg/fmt/#Print),
[`fmt.Printf`](https://golang.org/pkg/fmt/#Printf), and [`fmt.Println`](https://golang.org/pkg/fmt/#Println)
functions. See the [format documentation](https://golang.org/pkg/fmt/#hdr-Printing)
for details on `printf`'s format syntax.

### Operators

And the following comparison operators are also supported:

- `eq`: Equal (`==`)
- `ne`: Not-equal (`!=`)
- `lt`: Less than (`<`)
- `le`: Less than or equal to (`<=`)
- `gt`: Greater than (`>`)
- `ge`: Greater than or equal to (`>=`)

### Actions

There are also a few _actions_, which are used for control flow and other purposes. See [the documentation](https://golang.org/pkg/text/template/#hdr-Actions) for details on these:

- `if`/`else`/`else if`: Conditional control flow.
- `with`/`else`: Conditional execution with assignment.
- `range`: Looping control flow. See discussion in the [Arrays](../syntax/#arrays) and [Maps](../syntax/#maps) sections.
  - `break`: The innermost `range` loop is ended early, stopping the current iteration and bypassing all remaining iterations.
  - `continue`: The current iteration of the innermost `range` loop is stopped, and the loop starts the next iteration.
- `template`: Include the output of a named template. See the [Nested templates](/syntax/#nested-templates) section for more details, and the [`tmpl`](../functions/tmpl) namespace for more flexible versions of `template`.
- `define`: Define a named nested template. See the [Nested templates](/syntax/#nested-templates) section for more details.
- `block`: Shorthand for `define` followed immediately by `template`.

## gomplate functions

gomplate provides over 200 functions not found in the standard library. These
are grouped into namespaces, and documented on the following pages:

{{% children depth="3" description="false" %}}

[`text/template`]: https://golang.org/pkg/text/template/
[`base64.Encode`]: ../functions/base64#base64encode
[data sources]: ../datasources/