Template formatting functions
Format answers before using them in templated strings.
Using template functions
Sometimes, your answer will need formatting before it can be used in a template.
For example, you might have an API that returns an answer called product_price
as a number, using an API answer.
If we used product_price directly, it wouldn't look good for some answers, since we're used to seeing money formatted using commas.
When the product_price is Would render as:
To address this, you can use template functions. Template functions come after the answer variable, and are separated using the pipe character (|
). Here, we use the usd
function to show the price as US dollars:
Would render as:
Much better.
Chaining template functions
Template functions can be chained togetherโjust separate subsequent template functions with the |
character. Continuing the above example, we could round
the value before displaying it as usd
so that we have a nice round price.
Both functions are applied, and we get:
Available functions
capitalize
capitalize
Capitalizes a string (upper case the first letter).
ceil
ceil
Rounds up a number to the nearest integer.
default "some-text"
default "some-text"
Sets default text if the templated variable is undefined, instead of the default, which is to show the variable name in curly brackets.
For example, if I have a template like Allergies: {{allergies | default "none"}}
then Allergies: none
will be shown if the variable is not defined. If defined, the variable wil render like it would normally.
The default text shown will not set the answer value for the variable.
It's possible to use the empty string (""
) as the default if you would like nothing to be displayed.
floor
floor
Rounds down a number to the nearest integer.
get
get
Gets a value from an object (such as an Address answer), at a specific key.
The key is specified as the second parameter:
label
label
returns the choice label, rather than the value
lowercase
lowercase
lowercases a string.
round
round
Rounds a number to the nearest integer, up or down.
uppercase
uppercase
UPPERCASES a string.
usd
usd
Displays a string as US dollars.
Not seeing what you need?
Consider defining a calculated answer that formats your answer into what you need.
Or, if you think your use case would be generally useful, drop us a note in the chat!
Last updated