Handlebars Guide

Mailtrap Email Templates support the Handlebars templating language. It combines an input object (JSON) and a template to create text formats, HTML, or an email subject.

You can use Handlebars syntax to personalize your email templates and insert specific information for each of your recipients.

For instance, you may have a client business name called "business name" under a JSON object property. Then, using {{business name}} Handlebars expression somewhere in the template, you can insert the property value to an email template.

The main benefit of using this syntax is that you don't have to update your code base. The dynamic templating happens fast and outside the code base.

Supported Handlebars features

Handlebars supports a bunch of features, not just the variable replacement. Mailtrap templates support the standard set of helpers:

  • Basic replacement

  • Conditional statements: {{#if}}, {{#unless}}, {{#each}}, and {{#with}}

The following sections give you examples for each of the helpers including code blocks, mock JSON data, and HTML output.

Basic replacement

The basic usage is just to render the values you pass. You can use objects and refer to variables like {{object_name.variable}}.

If the variable is present in the data you pass, it won't be rendered.

If you want to do a replacement with HTML, use triple brackets {{{value_with_html}}}.

Template

<p>Hello {{firstName}} {{lastName}}!</p>
<p>Click this <a href="{{url}}">link</a></p>
<p>{{company.name}}, {{company.adress}}</p>
<p>{{{signature}}}</p>

JSON Object

HTML output

Conditional Statements

if / else / if else

Use the if helper for conditional block rendering.

Should the argument return "", 0, nil, false, empty_map, empty_slice, or an empty_array, then the block won't be rendered.

Template

JSON test data

HTML email output

If you change the isSubscribed variable to false in JSON data, the HTML output is:

The examples above include both if and else expressions. Of course, you can use only if, but it's recommendable to include else as well to cover the scenario where a conditional statement is false.

unless

The unless block helper works like an inverse if. Simply put, it renders when the expression returns a false value.

Template

JSON test data

HTML email output

The block helper example also contains {{else}} and should the isSubscribed variable be true, here's the HTML output:

each

The {{#each}} helper is used to iterate over an object or array, then execute a block of code for each item.

In the example below, {{#each user.items}} iterate over the user.items array, then execute the code inside the block.

{{this}} helper is used as a reference to the current item in the iteration.

Template

JSON test data

HTML email output

Template with else block

JSON test data

HTML email output

with

You can use the with helper to change the context in which the code block gets executed.

In the example below, the {{with user}} block sets the context for the user object. Consequently, the {{name}} and {{email}} can be accessed directly. This is useful when you want to avoid writing long chains of nested property accessors.

Template

JSON test data

HTML email output

Template with else block

In the example below, the {{else}} clause only gets executed if there's no value within the {{with}} block.

JSON test data

HTML email output

Example: Order confirmation template

The following example contains the majority of Handlebars helpers explained above as well as mock JSON data, and HTML output.

Email template in HTML:

Mock JSON data:

HTML email output:

Testing templates with Handlebars

In the quick tutorial below, we assume you've activated both Email Sandbox and Email API/SMTP.

1

Navigate to Email API/SMTP > Email Templates in the menu on the left.

2

Select your email template and click the Integration tab.

3

Copy the code under the Integration tab (cURL, or any other based on your preference).

To test the template, you only need to change the Sending API endpoint (send.api.mailtrap.io) to Sandbox API (sandbox.api.mailtrap.io) and add the inbox_id to the end of the endpoint URL.

Integration page showing Transactional Stream and Bulk Stream options with Integrate buttons highlighted by red arrows
4

Run the template test and check the associated inbox to preview the template under sandbox.

Email Testing sandbox showing received test email with template content

Important notes:

  • Pay attention that the Authorization: Bearer (API token) token is related to the Inbox you're targeting. You can check (and copy-paste) the token under Settings > API Tokens.

  • Your inbox_id is in the Inbox URL.

For more details on template debugging, see Debugging.

Last updated

Was this helpful?