Template formatting functions
Format answers before using them in templated strings.
Last updated
Was this helpful?
Format answers before using them in templated strings.
Last updated
Was this helpful?
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 .
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.
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:
capitalize
Capitalizes a string (upper case the first letter).
ceil
Rounds up a number to the nearest integer.
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
Rounds down a number to the nearest integer.
get
The key is specified as the second parameter:
label
returns the choice label, rather than the value
lowercase
lowercases a string.
round
Rounds a number to the nearest integer, up or down.
uppercase
UPPERCASES a string.
usd
Displays a string as US dollars.
Gets a value from an object (such as an answer), at a specific key.
Consider defining a that formats your answer into what you need.