Skip to content

Using Built-in Helpers

The template rendering commands include a set of built-in helper functions to assist you with the rendering process.

Functions

include

 func include(template string, data any) string

This function allows you to invoke your own custom helper functions.

e.g.

{{ include "sayHello" $data }}

This function can also be used to render a file.

e.g.

{{ include "./path/to/file.yaml" . }}

The path to template file to render is relative to the parent template file.

os_writefile

func os_writefile(dest string, content string) string

Write a file to the output directory

The destination path is relative to the output directory. ( ".." or absolute paths are not allowed)

This function outputs the destination file path.

e.g.

{{ os_writefile "./dst/filename.txt" "contents" }}

os_copyfile

func os_copyfile(dest string, src string) string

Copies files to the output directory. This function outputs the destination file path as a string.

The destination path is relative to the output directory ( ".." or absolute paths are not allowed)

The source path is relative to the main template file directory ( ".." or absolute paths are not allowed)

e.g.

{{ os_copyfile "./dest/lib.jar" "./src/lib.jar" }}

os_getenvs

func os_getenvs() map[string]string

Gets all environment variables as a dictionary

e.g.

{{ $envs := os_getenvs }}

os_getenv

func os_getenv**(env string) string

Gets the value of the specified env var

e.g.

{{ os_getenv "USER" }}

slug_make

func slug_make(in string) string

Converts string to a slug

e.g.

{{ slug_maek "My API proxy" }}
The example above outputs "my-api-proxy"

url_parse

func url_parse(url string) net.URL

Parse a URL into its parts.

This function outputs a net.URL struct.

e.g.

{{ $url := url_parse "https://example.com/foo/bar" }}

blank

func blank() string

Outputs empty string. This is useful to consume the output of another function.

e.g.

{{ os_writefile "./dest/file" "foo" | blank }}

deref

func deref(*any) any

Dereferences the input pointer.

fmt_printf

func fmt_printf**(pattern string, args ... string)

Write to stdout during the rendering process. This function is useful for so called "printf" debugging.

For example, you can use it to trace the template rendering as it runs. Or, can also use it to dump values to stdout in order to see the contents.

e.g.

{{ fmt_printf "Hello World\n" }}

{{ fmt_printf "url: %%v\n" $url }}

Libraries

Sprig

This library contains a lot of useful functions for string manipulation, accessing maps, lists, encoding, and more.

Functions from Sprig library are available during rendering.

e.g.

{{ "Hello World" | upper }}

{{ list "hello" "world" | join "_" }}