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.

Price: {{product_price}}.

When the product_price is Would render as:

Price: 1499.95

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:

Price: {{product_price | usd}}.

Would render as:

Price: $1,499.95

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.

Price: {{product_price | round | usd}}.

Both functions are applied, and we get:

Price: $1,500

Available functions

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

Gets a value from an object (such as an Address answer), at a specific key.

The key is specified as the second parameter:

You live in the city of {{user_address | get "city"}}.

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.

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