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 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 and Maps.len
: Returns the length of the argument.print
,printf
,println
: Aliases for Go’sfmt.Print
,fmt.Printf
, andfmt.Println
functions. See the format documentation for details onprintf
’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 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 and Maps sections.break
: The innermostrange
loop is ended early, stopping the current iteration and bypassing all remaining iterations.continue
: The current iteration of the innermostrange
loop is stopped, and the loop starts the next iteration.
template
: Include the output of a named template. See the Nested templates section for more details, and thetmpl
namespace for more flexible versions oftemplate
.define
: Define a named nested template. See the Nested templates section for more details.block
: Shorthand fordefine
followed immediately bytemplate
.
gomplate functions
gomplate provides over 200 functions not found in the standard library. These are grouped into namespaces, and documented on the following pages:
- aws functions
- base64 functions
- collection functions
- conversion functions
- crypto functions
- data functions
- env functions
- file functions
- filepath functions
- gcp functions
- math functions
- net functions
- path functions
- random functions
- regexp functions
- semver functions
- sockaddr functions
- strings functions
- template functions
- test functions
- time functions
- uuid functions