Environments
This page documents how to access environment variables in your templates. There are two main ways to use environment variables:
- The
.Envvariable (direct access to all environment variables through a map) - The
envfunction (recommended for most cases since it supports default values)
Both approaches have their own use-cases and trade-offs. Read below to understand when to use each.
Variables¶
.Env¶
The .Env variable is a map containing all environment variables as key-value pairs. You can access variables directly using the map syntax.
Syntax¶
- If the environment variable is set, its value is returned.
- If not set, an error is raised.
Warning
There is no way to provide a default value using this method.
Examples¶
Usage¶
Functions¶
env¶
The env function allows you to read environment variables directly in your templates. It supports an optional default value, making it robust for missing variables.
Syntax¶
- If the environment variable is set, its value is returned.
- If not set, the default value (if provided) is returned.
- If not set and no default is provided, an error is raised.
Examples¶
Usage (without default value)¶
Usage (with default value)¶
Which Should I Use?¶
- Use
.Envfor direct access when you are certain the variable exists and want strict error handling. - Use the
envfunction if you want to provide a default value or handle missing variables gracefully.
Both methods are available so you can choose the best fit for your use-case.
Combination with Functions¶
You can combine environment variable access with template functions for advanced processing. The examples below show how to use .Env and env together with functions like parseBool and split to transform environment variable values directly in your templates.
.Env + parseBool¶
env + parseBool¶
env + split¶
if + env + parseBool¶
You can use a if with env and parseBool to conditionally insert content based on the value of an environment variable.
for + env + split¶
You can use a for loop with env and split to iterate over a list of values from an environment variable.
Tip
For a full list of available variables, see the Variables page.
Tip
For a full list of available functions, see the Functions page.