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.
Template
JSON test data
HTML email output
If you change the isSubscribed variable to false in JSON data, the HTML output is:
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.
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.
Navigate to Email API/SMTP > Email Templates in the menu on the left.
Select your email template and click the Integration tab.
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.

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

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_idis in the Inbox URL.
For more details on template debugging, see Debugging.
Last updated
Was this helpful?

