View a markdown version of this page

Using message template helpers - Amazon Pinpoint

End of support notice: On October 30, 2026, AWS will end support for Amazon Pinpoint. After October 30, 2026, you will no longer be able to access the Amazon Pinpoint console or Amazon Pinpoint resources (endpoints, segments, campaigns, journeys, and analytics). For more information, see Amazon Pinpoint end of support. Note: APIs related to SMS, voice, mobile push, OTP, and phone number validate are not impacted by this change and are supported by AWS End User Messaging.

Using message template helpers

With Amazon Pinpoint templates, customers can create reusable message templates based on the Handlebars.js language. Helpers provide a variety of features like formatting a price to a specific Region's currency or adding a time zone-based location. A helper can use a specific string or integer for the value or a specific Amazon Pinpoint message variable.

These are the categories of helpers, described in the following sections:

This section describes the built-in helpers provided by Handlebars. For the full list, see Built-in Helpers at handlebarsjs.com. These are the built-in helpers:

  • each – Iterates a list.

    Note

    The maximum list size is 15 items.

  • if – Evaluates a statement.

each

Iterates a list. This helper uses only a block statement. You can optionally:

  • Pass @index in the request to reference the current loop index.

  • Use the this helper to reference the current element being iterated.

  • Return the helper response in a list, using the <li> tag.

Usage

{{#each value}}

Value at position {{@index}} is {{this}}.

{{else}}

Condition is false.

{{/each}}

each must be prefaced with a pound sign (#) and conclude with a closing {{/each}} in the block statement.

Example

In this example, each is used to return a list of a user's favorite colors. For a false, an else statement is returned. If the request is this:

{{#each User.UserAttributes.FavoriteColors}}

<li>{{this}}</li>

{{else}}

You have no favorite colors.

{{/each}} returns

  • red

  • blue

  • yellow

for a true statement.

if

Evaluates whether something is true and returns a response based on the evaluation.

Usage

{{#if value}}

Value isn't undefined

{{else}}

Value is undefined

{{/if}}

if must be prefaced with a pound sign (#) and conclude with a closing {{/if}} in the block statement.

Example

In this example, the if helper is used to evaluate whether a user's first name. If the name is found, a greeting is returned that passes the user's first name in the response. Otherwise, the else statement returns an alternative greeting.

{{#if User.UserAttributes.FirstName.[0]}}

Hello {{User.UserAttributes.FirstName.[0]}},

{{else}}

Hello,

{{/if}}

returns Hello, Jane if the if helper is true.

This section describes the conditional helpers.

Conditional helpers can be used on either a single line or in a block statement. You can customize the response regardless of which helper method you use. You can pass additional conditional helpers within both single line and block statements. The following conditional helpers show usage first for a single line and then a block statement using an optional else clause. These are the conditional helpers:

  • and – Compares whether all passed elements are equal.

  • eq – Tests whether two elements are equal.

  • gt – Tests whether one element is greater than another.

  • gte – Tests whether one element is greater than or equal to another.

  • if – Evaluates whether something is true.

  • lt – Tests whether one element is less than another.

  • lte – Tests whether one element is less than or equal to another.

  • neq – Evaluates whether two elements are not equal.

  • not – Inverts the response of a Boolean operation.

  • or – Compares whether any of the elements in the argument are equal.

and

Compares whether all elements passed in an argument are equal, and then returns the response based on the result. This helper can be used for non-Boolean values. You must pass at least two elements for the condition.

Usage

  • {{and valuea valueb valuec valued yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#and valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/and}}

    and must be prefaced with a pound sign (#) and conclude with a closing {{/and}} in the block statement.

Example

In this example, eq is used within the and block statement to determine whether both strings passed for the Location.City and Location.Country attributes are true. If both conditions are equal, then a true statement is returned. If either of those attributes are false, then an else statement is returned.

{{#and (eq Location.City "Los Angeles") (eq Location.Country "US")}}

You live in Los Angeles and the US.

{{else}}

You don’t live in Los Angeles and the US.

{{/and}}

eq

Tests whether two elements are equal or if the value of one element is equal to a passed string.

Usage

  • {{eq valuea valueb yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#eq valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/eq}}

    eq must be prefaced with a pound sign (#) and conclude with a closing {{/eq}} in the block statement.

Example

In this example, eq is used to evaluate whether the value of User.UserAttributes.FavoriteColors.[0] is Red. If the response is true, a true statement is returned. If the response is false, then an else statement is returned.

{{#eq User.UserAttributes.FavoriteColors.[0] "red"}}

Your favorite color is red.

{{else}}

You don't like red.

{{/eq}}

gt

Tests whether the value of one element is greater than another.

Usage

  • {{gt valuea valueb yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#gt valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/gt}}

    gt must be prefaced with a pound sign (#) and conclude with a closing {{/gt}} in the block statement.

Example

In this example, the helper compares the value of User.UserAttributes.UserAge.[0] attribute against a string 17, to verify whether the user's age is greater than 17. If the response is true, a true statement is returned. If the response is false, then an else statement is returned.

{{#gt User.UserAttributes.UserAge.[0] "17"}}

You are old enough to rent a car.

{{else}}

You are not old enough to rent a car.

{{/gt}}

gte

Tests whether the value of one element is greater than or equal to another.

Usage

  • {{gte valuea valueb yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#gte valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/gte}}

    get must be prefaced with a pound sign (#) and conclude with a closing {{/gte}} in the block statement.

Example

In this example, the helper compares the User.UserAttributes.UserAge.[0] attribute against a string 18, to verify whether the user's age is greater than or equal to 18. If the response is true, a true statement is returned. If the response is false, then an else statement is returned.

{{#gte User.UserAttributes.UserAge.[0] "18"}}

You are old enough to rent a car.

{{else}}

You are not old enough to rent a car.

{{/gte}}

if

Evaluates whether something is true and returns a response based on the evaluation.

Usage

  • {{#if value}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#if value}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/if}}

    if must be prefaced with a pound sign (#) and conclude with a closing {{/if}} in the block statement.

Example

In this example, the helper is used to evaluate whether a user's first name. If the name is found, a greeting is returned that passes the user's first name in the response. Otherwise, the else statement returns an alternative greeting.

{{#if User.UserAttributes.FirstName.[0]}}

Hello {{User.UserAttributes.FirstName.[0]}},

{{else}}

Hello,

{{/if}}

returns Hello Jane, if the helper is true.

lt

Tests whether the value of one element is less than the value of another.

Usage

  • {{lt valuea valueb yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#lt valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/lt}}

    lt must be prefaced with a pound sign (#) and conclude with a closing {{/lt}} in the block statement.

Example

In this example, the helper compares the User.UserAttributes.UserAge.[0] attribute against a string 18 , to verify whether the user's age is less than 18. If the response is true, a true statement is returned. If the response is false, then an else statement is returned.

{{#lt User.UserAttributes.UserAge.[0] "18"}}

You are not old enough to rent a car.

{{else}}

You are old enough to rent a car.

{{/lt}}

lte

Tests whether the value of an element is less than or equal to another.

Usage

  • {{lte valuea valueb yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#lte valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/lte}}

    lte must be prefaced with a pound sign (#) and conclude with a closing {{/lte}} in the block statement.

Example

In this block statement, the helper compares the User.UserAttributes.UserAge.[0] attribute against a string 17, to verify whether the user's age is equal to 17 or younger. If the response is true, a true statement is returned. If the response is false, then an else statement is returned.

{{#lte User.UserAttributes.Age.[0] "17"}}

You are not old enough to rent a car.

{{else}}

You are old enough to rent a car.

{{/lte}}

neq

Test whether two elements are not equal.

Usage

  • {{neq valuea valueb yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#neq valuea valueb}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/neq}}

    neq must be prefaced with a pound sign (#) and conclude with a closing {{/neq}} in the block statement.

Example

In this block statement, the User.UserAttributes.FavoriteColors.[0] attribute is checked against a string Red. If the response is true, a true statement is returned. If the response is false, then an else statement is returned.

{{#neq User.UserAttributes.Favorite.Colors.[0] "red"}}

You do not like red.

{{else}}

You like red.

{{/neq}}

not

Inverts the response of a Boolean operation, so that if not is a positive comparison, then a true statement is returned. If the response is false, then an else statement is returned.

Usage

  • {{not value yes='y' no='n'}}

    You can replace y and n with other values, such as yes and no, or any other string you want returned, depending on the condition.

  • {{#not value}}

    Condition is true.

    {{else}}

    Condition is false.

    {{/not}}

    not must be prefaced with a pound sign (#) and conclude with a closing {{/not}} in the block statement.

Example

In this block statement, the User.UerAttributes.FavoriteColors.[0] attribute is checked against a string red, using the eq helper. The not helper then returns the opposite of the eq helper. If the response returns any color other than red, a true a statement is returned. If the response returns red, then an else statement is returned indicating a false statement.

{{#not (eq User.UserAttributes.Favorite.Colors.[0] "red")}}

You do not like red.

{{else}}

You like red.

{{/not}}

Example

In this example,

{{not (eq User.UserAttributes.FavoriteColors.[0] "red")}}

returns false if User.UserAttributes.FavoriteColors.[0] is red.

or

Compares whether any of the elements in the argument are equal, and then returns a response based on the result. This helper can be used for non-Boolean values.

Usage