Use Handlebar Helpers

Optimize the configuration of your email templates by using handlebar helpers. Add logic into your email templates by using conditional helpers. You can also use other handlebar helpers such as string, number, and date formats.

Conditional Helpers

Some common conditional function helpers with examples are listed in the table below:

Function Description Example
eq

Checks if two elements are equal.

This example shows how to modify an email's subject based on a task's outcome (approve or reject)

Task {{taskNumber}} created by {{creator}} is {{#eq outcome "APPROVE"}}approved successfully{{else}} rejected{{/eq}}
neq

Checks if two elements are not equal.

This example shows how to compare a business object’s value and modify an email.

{{#eq outcome "APPROVE"}}
    <p>Hello {{payload.expenseApprovalForm.firstName}}, your expense report for payload.expenseApprovalForm.totalAmount}} was approved!</p>
{{else}}
    <p>Hello {{payload.expenseApprovalForm.firstName}}, Sorry your expense report for {{payload.expenseApprovalForm.totalAmount}} was rejected!</p>
{{/eq}}

{{#neq payload.expenseApprovalForm.team "sales"}}
    Sorry, this item is only eligible for Sales team
{{/neq}}
gt

Greater than operator

The example below shows how to mark an email's subject as Important! if the total amount of an expense report is greater than $10,000.

{{#gt payload.expenseApprovalForm.totalAmount 10000}}Important!{{/gt}} Expense report by {{creator}} for amount {{payload.expenseApprovalForm.totalAmount}} for approval

The example below compares an array object's size and conditionally shows expense items only if the number of items is greater than 1.

{{#gt payload.expenseApprovalForm.items.length 1}}
    <h3>Expense Items</h3>
    <table>
      <thead>
        <tr>
          <th>Item</th>
          <th>Amount</th>
        </tr>
    </thead>
    <tbody>
      <tr>
        <td>item1</td>
        <td>400</td>
      </tr>
      <tr>
        <td>item2</td>
        <td>340</td>
      </tr>
    </tbody>
    </table>
{{/gt}}
and

Checks if two conditions are true

This example shows how to add a special category section into the email's content if the travel category is critical and if the traveler is an executive.

{{#and payload.travelApproval.isCritical payload.travelApproval.traveler.isExecutive}}
  <h3>Special Category Traveler</h3>
{{else}}
  <h3>Normal Traveler</h3>
{{/and}} 

String, Date, and Number Format Helpers

Some common string, date, and number format helpers with examples are listed in the table below:

Function Description Example
capitalizeFirst

Capitalizes the first character of the value.

 <pre>
 {{capitalizeFirst value}}
 </pre>

If value is "string.example", the output will be "String.example".

cut

Removes all values of arg from the given string.

<pre>
{{cut value [" "]}}
</pre>

If value is "String with spaces", the output will be "Stringwithspaces".

join

Joins an array, iterator or an iterable with a string.

<pre>
{{join value " // " [prefix=""] [suffix=""]}}
</pre>

If value is the list ['a', 'b', 'c'], the output will be the string "a // b // c".

lower

Converts a string into all lower case.

<pre>
{{lower value}}
</pre>

If value is 'String Helper Example', the output will be 'string helper example'.

upper

Converts a string into all upper case.

If value is 'Hello', the output will be 'HELLO'.

replace

Replaces each substring of a string that matches the literal target sequence with the specified literal replacement sequence.

<pre>
{{ replace value "..." "example" }}
</pre>

If value is "String ...", the output will be "String example".

dateFormat

Returns the date in the specified format.

Parameters:

  • full
  • long
  • medium
  • short
  • pattern
<pre>
{{dateFormat date ["format"] [format="format"][tz=timeZone|timeZoneId]}}
</pre>
  

Examples:

  • {{dateFormat date ["full"]}

    output: Tuesday, June 19 2012

  • {{dateFormat date ["long"]}

    output: June 19, 2012

  • {{dateFormat date ["medium"]}

    output: Jun 19, 2012

  • {{dateFormat date ["short"]}

    output: 6/19/12

numberFormat

Returns the number in the specified format.

Parameters:

  • integer
  • percent
  • currency
  • pattern
<pre>
{{numberFormat number ["format"] [locale=default]}}
</pre>

Note:

Handlebars is a superset of Mustache and Mustache templates are compatible with Handlebars.

See Handlebars.