github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/_index.md (about) 1 --- 2 title: Functions 3 --- 4 5 Almost all of gomplate's utility is provided as _functions._ These are key 6 words that perform some action. 7 8 For example, the [`base64.Encode`][] function will encode some input string as 9 a base-64 string: 10 11 ``` 12 The word is {{ base64.Encode "swordfish" }} 13 ``` 14 renders as: 15 ``` 16 The word is c3dvcmRmaXNo 17 ``` 18 19 ## Built-ins 20 21 Go's [`text/template`][] language provides a number of built-in functions, 22 operators, and actions that can be used in templates. 23 24 ### Built-in functions 25 26 Here is a list of the built-in functions, but see [the documentation](https://golang.org/pkg/text/template/#hdr-Functions) 27 for full details: 28 29 - `and`, `or`, `not`: Returns boolean AND/OR/NOT of the argument(s). 30 - `call`: Returns the result of calling a function argument. 31 - `html`, `js`, `urlquery`: Safely escapes input for inclusion in HTML, JavaScript, and URL query strings. 32 - `index`: Returns the referenced element of an array/slice, string, or map. See also [Arrays](#arrays) and [Maps](#maps). 33 - `len`: Returns the length of the argument. 34 - `print`, `printf`, `println`: Aliases for Go's [`fmt.Print`](https://golang.org/pkg/fmt/#Print), 35 [`fmt.Printf`](https://golang.org/pkg/fmt/#Printf), and [`fmt.Println`](https://golang.org/pkg/fmt/#Println) 36 functions. See the [format documentation](https://golang.org/pkg/fmt/#hdr-Printing) 37 for details on `printf`'s format syntax. 38 39 ### Operators 40 41 And the following comparison operators are also supported: 42 43 - `eq`: Equal (`==`) 44 - `ne`: Not-equal (`!=`) 45 - `lt`: Less than (`<`) 46 - `le`: Less than or equal to (`<=`) 47 - `gt`: Greater than (`>`) 48 - `ge`: Greater than or equal to (`>=`) 49 50 ### Actions 51 52 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: 53 54 - `if`/`else`/`else if`: Conditional control flow. 55 - `with`/`else`: Conditional execution with assignment. 56 - `range`: Looping control flow. See discussion in the [Arrays](#arrays) and [Maps](#maps) sections. 57 - `break`: The innermost `range` loop is ended early, stopping the current iteration and bypassing all remaining iterations. 58 - `continue`: The current iteration of the innermost `range` loop is stopped, and the loop starts the next iteration. 59 - `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`. 60 - `define`: Define a named nested template. See the [Nested templates](/syntax/#nested-templates) section for more details. 61 - `block`: Shorthand for `define` followed immediately by `template`. 62 63 ## gomplate functions 64 65 gomplate provides over 200 functions not found in the standard library. These 66 are grouped into namespaces, and documented on the following pages: 67 68 {{% children depth="3" description="false" %}} 69 70 [`text/template`]: https://golang.org/pkg/text/template/ 71 [`base64.Encode`]: ../functions/base64#base64-encode 72 [data sources]: ../datasources/