Skip to main content

Function

_

_ is a no-op function that returns an empty string. It's useful to place a newline in the template.

Example:

{{- if true -}}
{{_}} ok
{{- end}}

Output:

ok

Container

dict

dict creates a map from the given key/value pairs.

  • Parameters: (dict_or_pairs: ...any)

dict supports keys of any comparable type and values of any type. If an odd number of arguments is provided, it returns an error. If the first argument is already a map, it extends that map with the following key/value pairs.

Example:

{{$user := dict "name" "Alice" "age" 30}}
{{dict "user" ($user) "active" true}}

Output:

map[user:map[name:Alice age:30] active:true]

first

first returns the first element of a list or string.

Example:

{{first (list 1 2 3)}}
{{first "hello"}}

Output:

1
h

includes

includes checks if an item is present in a list, map, or string.

  • Parameters: (item: any, collection: slice | map | string)
  • Returns: bool

Example:

{{includes 2 (list 1 2 3)}}
{{includes "world" "hello world"}}

Output:

true
true

last

last returns the last element of a list or string.

Example:

{{last (list 1 2 3)}}
{{last "hello"}}

Output:

3
o

list

list creates a list from the given arguments.

Example:

{{list 1 2 3}}

Output:

[1 2 3]

map

map maps a list of values using the given function and returns a list of results.

  • Parameters: (fn: function, list: slice)

Example:

{{list 1 2 3 | map (add 1)}}
{{list "a" "b" "c" | map (upper | replace "A" "X")}}
{{"math/rand.Int63, *io.Reader, *io.Writer" | split "," | map (trim | split "." | first | trimPrefix "*") | sort | uniq}}

Output:

[2 3 4]
[X B C]
[io math/rand]

reverse

reverse reverses a list or string.

Example:

{{reverse (list 1 2 3)}}
{{reverse "hello"}}

Output:

[3 2 1]
olleh

sort

sort sorts a list of numbers or strings.

Example:

{{sort (list 3 1 4 1 5 9)}}
{{sort (list "banana" "apple" "cherry")}}

Output:

[1 1 3 4 5 9]
[apple banana cherry]

uniq

uniq removes duplicate elements from a list.

Example:

{{uniq (list 1 2 2 3 3 3)}}

Output:

[1 2 3]

Convert

bool

bool converts a value to a boolean.

Example:

{{bool 1}}
{{bool "false"}}

Output:

true
false

float

float converts a value to a float.

Example:

{{float "3.14"}}
{{float 42}}

Output:

3.14
42

int

int converts a value to an integer.

Example:

{{int "42"}}
{{int 3.14}}

Output:

42
3

string

string converts a value to a string.

Example:

{{string 42}}
{{string true}}

Output:

42
true

Date

now

now returns the current time.

Example:

{{now}}

Output:

2024-09-12 15:04:05.999999999 +0000 UTC

parseTime

parseTime parses a time string using the specified layout.

  • Parameters: (layout: string, value: string)

Example:

{{parseTime "2006-01-02" "2024-09-12"}}

Output:

2024-09-12 00:00:00 +0000 UTC

Encoding

b64dec

b64dec decodes a base64 encoded string.

Example:

{{b64dec "SGVsbG8sIFdvcmxkIQ=="}}

Output:

Hello, World!

b64enc

b64enc encodes a string to base64.

Example:

{{b64enc "Hello, World!"}}

Output:

SGVsbG8sIFdvcmxkIQ==

Math

add

add adds two numbers.

  • Parameters: (a: number, b: number)

Example:

{{add 2 3}}

Output:

5

ceil

ceil returns the least integer value greater than or equal to the input.

Example:

{{ceil 3.14}}

Output:

4

floor

floor returns the greatest integer value less than or equal to the input.

Example:

{{floor 3.14}}

Output:

3

max

max returns the maximum of a list of numbers.

  • Parameters: numbers (variadic)

Example:

{{max 3 1 4 1 5 9}}

Output:

9

min

min returns the minimum of a list of numbers.

  • Parameters: numbers (variadic)

Example:

{{min 3 1 4 1 5 9}}

Output:

1

mod

mod returns the modulus of dividing the first number by the second.

  • Parameters: (a: number, b: number)

Example:

{{mod -7 3}}

Output:

2

mul

mul multiplies two numbers.

  • Parameters: (a: number, b: number)

Example:

{{mul 2 3}}

Output:

6

quo

quo divides the first number by the second.

  • Parameters: (a: number, b: number)

Example:

{{quo 6 3}}

Output:

2

rem

rem returns the remainder of dividing the first number by the second.

  • Parameters: (a: number, b: number)

Example:

{{rem 7 3}}

Output:

1

round

round rounds a number to a specified number of decimal places.

  • Parameters: (precision: integer, value: number)

Example:

{{round 2 3.14159}}

Output:

3.14

sub

sub subtracts the second number from the first.

  • Parameters: (a: number, b: number)

Example:

{{sub 5 3}}

Output:

2

OS

absPath

absPath returns the absolute path of a file or directory.

Example:

{{absPath "file.txt"}}

Output:

/path/to/file.txt

basename

basename returns the last element of a path.

Example:

{{basename "path/to/file.txt"}}

Output:

file.txt

cleanPath

cleanPath returns the cleaned path.

Example:

{{cleanPath "path/to/../file.txt"}}

Output:

path/file.txt

dirname

dirname returns the directory of a path.

Example:

{{dirname "path/to/file.txt"}}

Output:

path/to

extname

extname returns the extension of a path.

Example:

{{extname "path/to/file.txt"}}

Output:

.txt

glob

glob returns the names of all files matching a pattern.

Example:

{{glob "/path/to/*.txt"}}

Output:

[/path/to/file1.txt /path/to/file2.txt]

isAbs

isAbs reports whether a path is absolute.

Example:

{{isAbs "/path/to/file.txt"}}

Output:

true

joinPath

joinPath joins path elements into a single path.

  • Parameters: elements (variadic)

Example:

{{joinPath "path" "to" "file.txt"}}

Output:

path/to/file.txt

matchPath

matchPath reports whether a path matches a pattern.

  • Parameters: (pattern: string, path: string)

Example:

{{matchPath "/path/to/*.txt" "/path/to/file.txt"}}

Output:

true

relPath

relPath returns the relative path between two paths.

  • Parameters: (base: string, target: string)

Example:

{{relPath "/path/to" "/path/to/file.txt"}}

Output:

file.txt

splitPath

splitPath splits a path into its elements.

Example:

{{splitPath "path/to/file.txt"}}

Output:

[path to file.txt]

Strings

camelCase

camelCase converts a string to camelCase.

Example:

{{camelCase "hello world"}}

Output:

helloWorld

capitalize

capitalize capitalizes the first character of a string.

Example:

{{capitalize "hello"}}

Output:

Hello

center

center centers a string in a field of a given width.

  • Parameters: (width: int, target: string)

Example:

{{center 20 "Hello"}}

Output:

"       Hello        "

hasPrefix

hasPrefix checks if a string starts with a given prefix.

  • Parameters: (prefix: string, target: string)
  • Returns: bool

Example:

{{hasPrefix "Hello" "Hello, World!"}}

Output:

true

hasSuffix

hasSuffix checks if a string ends with a given suffix.

  • Parameters: (suffix: string, target: string)
  • Returns: bool

Example:

{{hasSuffix "World!" "Hello, World!"}}

Output:

true

html

html escapes special characters in a string for use in HTML.

Example:

{{html "<script>alert('XSS')</script>"}}

Output:

&lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;

join

join joins a slice of strings with a separator.

  • Parameters: (separator: string, values: slice of strings)
  • Returns: string

Example:

{{join "-" (list "apple" "banana" "cherry")}}

Output:

apple-banana-cherry

kebabCase

kebabCase converts a string to kebab-case.

Example:

{{kebabCase "helloWorld"}}

Output:

hello-world

lower

lower converts a string to lowercase.

Example:

{{lower "HELLO"}}

Output:

hello

matchRegex

matchRegex checks if a string matches a regular expression.

  • Parameters: (pattern: string, target: string)
  • Returns: bool

Example:

{{matchRegex "^[a-z]+$" "hello"}}

Output:

true

pascalCase

pascalCase converts a string to PascalCase.

Example:

{{pascalCase "hello world"}}

Output:

HelloWorld

quote

quote returns a double-quoted string.

Example:

{{print "hello"}}
{{quote "hello"}}

Output:

hello
"hello"

repeat

repeat repeats a string a specified number of times.

  • Parameters: (count: int, target: string)

Example:

{{repeat 3 "abc"}}

Output:

abcabcabc

replace

replace replaces all occurrences of a substring with another substring.

  • Parameters: (old: string, new: string, target: string)

Example:

{{replace "o" "0" "hello world"}}

Output:

hell0 w0rld

replaceN

replaceN replaces the first n occurrences of a substring with another substring.

  • Parameters: (old: string, new: string, n: int, target: string)

Example:

{{replaceN "o" "0" 1 "hello world"}}

Output:

hell0 world

snakeCase

snakeCase converts a string to snake_case.

Example:

{{snakeCase "helloWorld"}}

Output:

hello_world

split

split splits a string by a separator.

  • Parameters: (separator: string, target: string)
  • Returns: slice of strings

Example:

{{split "," "apple,banana,cherry"}}

Output:

[apple banana cherry]

striptags

striptags removes HTML tags from a string.

Example:

{{striptags "<p>Hello <b>World</b>!</p>"}}

Output:

Hello World!

substr

substr extracts a substring from a string.

  • Parameters: (start: int, length: int, target: string)

Example:

{{substr 0 5 "Hello, World!"}}

Output:

Hello

trim

trim removes leading and trailing whitespace from a string.

Example:

{{trim "  hello  "}}

Output:

hello

trimPrefix

trimPrefix removes a prefix from a string if it exists.

  • Parameters: (prefix: string, target: string)

Example:

{{trimPrefix "Hello, " "Hello, World!"}}

Output:

World!

trimSuffix

trimSuffix removes a suffix from a string if it exists.

  • Parameters: (suffix: string, target: string)

Example:

{{trimSuffix ", World!" "Hello, World!"}}

Output:

Hello

truncate

truncate truncates a string to a specified length and adds a suffix if truncated.

  • Parameters: (length: int, suffix: string, target: string)

Example:

{{truncate 10 "..." "This is a long sentence."}}

Output:

This is a...

unquote

unquote returns an unquoted string.

Example:

{{unquote "\"hello\""}}

Output:

hello

upper

upper converts a string to uppercase.

Example:

{{upper "hello"}}

Output:

HELLO

urlEscape

urlEscape escapes a string for use in a URL query.

Example:

{{urlEscape "hello world"}}

Output:

hello+world

urlUnescape

urlUnescape unescapes a URL query string.

Example:

{{urlUnescape "hello+world"}}

Output:

hello world

wordwrap

wordwrap wraps words in a string to a specified width.

  • Parameters: (width: int, target: string)

Example:

{{wordwrap 10 "This is a long sentence that needs wrapping."}}

Output:

This is a
long
sentence
that needs
wrapping.