Handlebars Helpers
Courier's custom Handlebars helpers for use in email template overrides and custom brand templates.
The default Handlebars helpers can be found here.
Courier Handlebars Helpers
Don't see a custom handlebars helper you need in the list? Share your use case.
Universal Helpers
and
Returns true
if all arguments are truthy, false
if one argument is falsy, and true
if no arguments
Parameters
input
{String|Number }
Note: Data and profile can have booleans.
Example:
capitalize
Returns the capitalized first character of a given string.
Parameters
input
{string}
Example
concat
Stringifies all arguments and joins them using the provided separator. Should use null
string for the separator by default and should return a null string if no arguments.
Parameters
input
{String|Number}
Example
condition
Use the if statement to specify a block of code to be executed if a condition is true. Will throw an error if the operator is unsupported.
- Require two operands separated by an operator.
- Run the appropriate operation on the operands.
Parameters
input
{String|Number}
Note: Data and profile can have booleans.
Example:
conditional
Loops through all arguments and ensures all are truthy if using the "and" logical operator.
Loops through all arguments and ensures one is truthy if using the "or" logical operator.
Returns true if the conditions passed, false otherwise (use #unless helper to hide content if condition passes).
input
{String|Number}
Note: Data and profile can have booleans.
default
Returns the value
if it is not nullish.
Parameters
input
{String|Number}
Note: Data and profile can have booleans.
Example:
each
Iterates over a list. Inside the block, you can use this
to reference the element being iterated over.
Parameters
input
{String|Number}
Example:
"data": {
"people": [
{
"name": "Rod"
},
{
"name": "TK"
},
{
"name": "Suhas"
}
]
}
Note
This is a modified version of this Handlebars helper.
format
Returns a formatted string given a format and set of arguments.
Parameters
input
{String | Number}
Example:
datetime-format
Returns a formatted date string, can be set to a user IANA Timezone This helper will throw if an invalid date is provided.
Parameters
input
{String|Number}
Examples
info
See full list of format options along with an additional option %p to add am, pm
inc
Increase value by 1.
Parameters
input
{Number}
Example:
not
Usage:
{{#if (and (not false) true)}}
Should:
- Return a
boolean
inverse of the value.
or
Usage:
{{#if (or true false)}}
Should:
- Return
true
if one argument is truthy. - Return
false
if all argument are falsy. - Return
false
if no arguments.
path
Usage:
{{#if (path "customer.email")}}
Should:
- Use the variable handler to resolve a
JSONPath
. - Return the resolved value.
- Return undefined if the value was not found.
set
Usage:
{{set "name" (default first_name "valued customer")}}
Should:
- Set a value on the root data using the name.
- Set the value on the root of the variable handler context.
- Restrict top level names (ex:
data
,profile
,brand
).
trim-left
Usage:
{{#trim-left}} test {{/trim-left}}
{{trim-left " a string "}}
Should:
- Return the value trimmed (defaults to block value).
trim-one-char-right
Usage:
{{a-block (params text=(trim-one-char-right "ends with\ "))}}
Should:
- Return the string with the last character removed.
info
This helper is for dealing with the following escaping issue.
trim-right
Usage:
{{#trim-right}} test {{/trim-right}}
{{trim-right " a string "}}
Should:
- Return the value trimmed (defaults to block value).
trim
Usage:
{{#trim}} test {{/trim}}
{{trim " a string "}}
Should:
- Return the value trimmed (defaults to block value)
truncate
Usage:
{{ truncate "some very long string" 9 }} => "some very"
{{ truncate "some very long string" 9 "..." }} => "some very..."
Should:
- Truncate the string to the number of characters defined in the helper
var
Usage:
"{{var customer.fullName}}" <{{var "customer.email"}}>
Should:
- Use the variable handler to resolve a
JSONPath
. - Return the resolved value as a string.
- Not be a
SafeString
so the value will be encoded.
with
Usage:
{{with customers}}
Should:
- Match the Handlebars built-in #with helper.
- Set the
@variableHandler
scope when rendering the block content.
Note
modified version of this Handlebars helper.
Math
abs
Return the magnitude of a number. This helper will throw an errow if it encounters a value that is not a number.
Parameters
input
{Number}
Example
add
Returns the the sum of two operands. This helper will throw an error if it encounters an input value that is not a number.
Parameters
term
{Number}term
{Number}
Example
ceil
Returns the least integer greater than or equal to the decimal number input. This helper will throw an errow if it encounters a value that is not a number.
Parameters
input
{Number}
Example
divide
Returns the quotient of its operands where the left operand is the dividend and the right operand is the divisor. This helper will throw an errow if it encounters an input value that is not a number or if the divisor is 0
.
Parameters
dividend
{Number}divisor
{Number}
Example
floor
Returns the largest integer less than or equal to a given number. This helper will throw an errow if it encounters a value that is not a number.
Parameters
input
{Number}
Example
mod
Returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
This helper will throw an errow if it encounters an input value that is not a number or if the divisor is 0
.
Parameters
dividend
{Number}divisor
{Number}
Usage:
Example
multiply
Returns the product of two operands. This helper will throw an errow if it encounters an input value that is not a number.
Parameters
multiplier
{Number}multiplicand
{Number}
Example
round
Returns the value of a number rounded to the nearest integer. This helper will throw an errow if it encounters an input value that is not a number.
Parameters
input
{Number}
Example
subtract (sub)
Returns the difference of two operands. This helper will throw an errow if it encounters an input value that is not a number.
Parameters
term
{Number}term
{Number}
Example
SendWithUs Helpers
SendWithUs helpers are available to help ease migration from your existing Jinja templates to Courier rendered messages.
swu_datetimeformat
Returns a formatted date string. This helper will throw if an invalid date is provided.
Parameters
input
{String|Number}
Examples
swu_iso8601_to_time
Returns an ISO-8601 date string as epoch milliseconds. This helper will throw if an invalid date is provided.
Parameters
input
{String}
Examples
swu_timestamp_to_time
Returns an Unix epoch timestamp (seconds) as a millisecond epoch time value. This helper will throw if an invalid unix epoch date is not provided.
Parameters
input
{Number}
Examples
translate
Translates a string based on the key, profile locale and .po translations you have uploaded to Courier.
Parameters
key
{String}args
{String} (multiple, space separated arguments)domain
{String} (only scoped to "default" right now)locale
{String} (fetched automatically from profile right now)
Examples
Common use-cases
Conditionally rendering content
#if
blocks can be used to control whether content is rendered.
Example
Pluralizing Text
The formatMessage
helper can be used to pluralize text in rendered content. Pluralization respects the ICU format and entries follow Unicode CLDR rules.
Example