Skip to content

Functions

This page documents the built-in functions available for use in your template files. These functions can be called directly within your Go templates to insert and transform data, perform checks, or parse values.

Note

The current set of helper functions is limited, more functions may be added in the future. If you need additional helper functions, feel free to request it!

Parsing (String Conversion)

These functions help you to convert your inserted data to the correct type.

Note

All external input (e.g. environment) values start as strings.

parseBool

Parses a string as a boolean value.

Syntax

{{ parseBool <string> }}

Examples

Usage
Template
{{ parseBool "true" }}
Output
true

parseInt

Parses a string as an integer.

Syntax

{{ parseInt <string> <base> <bitSize> }}

Examples

Usage
Template
{{ parseInt "42" 10 0 }}
Output
42

parseFloat

Parses a string as a floating-point number.

Syntax

{{ parseFloat <string> <bitSize> }}

Examples

Usage
Template
{{ parseFloat "3.14" 64 }}
Output
3.14

parseTime

Parses a string as a time value using the specified layout.

Syntax

{{ parseTime <string> <layout> }}

Examples

Usage
Template
{{ parseTime "2024-06-01T12:34:56Z" "2006-01-02T15:04:05Z07:00" }}
Output
2024-06-01 12:34:56 +0000 UTC

Formatting (String Conversion)

These functions help you to convert values to their string representations.

formatBool

Formats a boolean value as a string.

Syntax

{{ formatBool <bool> }}

Examples

Template
{{ formatBool true }}
Output
true

formatInt

Formats an integer value as a string in the given base.

Syntax

{{ formatInt <int> <base> }}

Examples

Template
{{ formatInt 42 10 }}
Output
42

formatFloat

Formats a floating-point value as a string with the given format, precision, and bit size.

Syntax

{{ formatFloat <float> <fmt> <prec> <bitSize> }}

Examples

Template
{{ formatFloat 3.14 "f" 2 64 }}
Output
3.14

formatTime

Formats a time value using the specified layout.

Syntax

{{ formatTime <time> <layout> }}

Examples

Template
{{ formatTime (parseTime "2024-06-01T12:34:56Z" "2006-01-02T15:04:05Z07:00") "2006-01-02" }}
Output
2024-06-01

String

These functions help you to transform your inserted data.

lower

Converts a string to lowercase.

Syntax

{{ lower <string> }}

Examples

Usage
Template
{{ lower "Hello World" }}
Output
hello world

upper

Converts a string to uppercase.

Syntax

{{ upper <string> }}

Examples

Usage
Template
{{ upper "Hello World" }}
Output
HELLO WORLD

split

Splits a string into a slice using the given separator.

Syntax

{{ split <string> <separator> }}

Examples

Usage
Template
{{ split "a,b,c" "," }}
Output
[a b c]

replace

Replaces all occurrences of a substring with another substring. The last argument is the number of replacements to make. Use -1 to replace all occurrences.

Syntax

{{ replace <string> <old> <new> <n> }}

Examples

Usage
Template
{{ replace "foo bar foo" "foo" "baz" -1 }}
Output
baz bar baz

File System

exists

Checks if a file or directory exists at the given path.

Syntax

{{ exists <path> }}

Examples

Usage
Template
{{ exists "/etc/passwd" }}
Output
true

Tip

To access environment variables within your templates, use the dedicated env function or its .Env variable counterpart.

For details and examples, see the dedicated Environments page.