Dynamic Templates
The Engagelab Email dynamic templates support the Handlebars template language to present the personalized content you send via the API.
Variable Replacement
Basic Replacement
Template Example
<p>Hello {{firstname}}</p>
Test Data
{ "firstName": "Engagelab" }
Rendering Result
<p>Hello Engagelab</p>
Nested Object Replacement
Template Example
<p>Hello {{user.profile.firstName}}</p>
Test Data
{
"user": {
"profile": {
"firstName": "Engagelab"
}
}
}
Rendering Result
<p>Hello Engagelab</p>
formatDate
The formatDate function accepts Unix timestamps or time in ISO8601 format and converts it into the format you specify using the markers in the following table.
Taking the UTC time 1987-09-20T20:55:00.000Z
as an example, the conversion and display results are shown in the table below:
Placeholder | Result |
---|---|
YYYY | 1987 |
YY | 87 |
MMMM | September |
MMM | Sep |
MM | 09 |
M | 9 |
DD | 20 |
D | 20 |
dddd | Sunday |
ddd | Sun |
hh | 08 |
h | 8 |
HH | 20 |
H | 20 |
mm | 55 |
m | 55 |
ss | 00 |
s | 0 |
A | PM |
ZZ | +0000 |
Z | +00:00 |
Template Example
<p>{{formatDate timeStamp dateFormat}}</p>
<!-- Example of time zone offset -->
<p>{{formatDate timeStamp dateFormat timezoneOffset}}</p>
Test Data
{
"timeStamp": "1987-09-20T12:55:00.000Z",
"dateFormat": "YYYY-MM-DD HH:mm:ss A",
"timezoneOffset": "+0800"
}
Rendering Result
<!-- Rendering Result(Template One) -->
<p>1987-09-20 12:55:00 PM</p>
<!-- Rendering Result(Template Two) -->
<p>1987-09-20 20:55:00 PM</p>
Insert
Template Example
<p>Thank you for contacting us about {{insert businessName "your business"}}.</p>
Test Data
// Test Data One
{}
// Test Data One
{
"businessName": "Engagelab"
}
Rendering Result
<!-- Rendering Result One -->
<p>Hello Ben! Thank you for contacting us about your business.</p>
<!-- Rendering Result二 -->
<p>Hello Customer! Thank you for contacting us about Engagelab.</p>
Conditional Statements
If, Else, Else If
Template Example
{{#if user.profile.isTeacher}}
<p>Hello, Teacher {{user.profile.firstName}}</p>
{{else if user.profile.isStudent}}
<p>Hello, Student {{user.profile.firstName}}</p>
{{else}}
<p>Hello, {{user.profile.firstName}}! Welcome.</p>
{{/if}}
Test Data
// Test Data One
{
"user": {
"profile": {
"firstName": "Zhang San",
"isTeacher": true
}
}
}
// Test Data二
{
"user": {
"profile": {
"firstName": "Li Si",
"isStudent": true
}
}
}
// Test Data三
{
"user": {
"profile": {
"firstName": "Wang Wu",
}
}
}
Rendering Result
<!-- Rendering Result(Test Data One) -->
<p>Hello, Teacher Zhang San</p>
<!-- Rendering Result(Test Data二) -->
<p>Hello, Student Li Si</p>
<!-- Rendering Result(Test Data三) -->
<p>Hello, Wang Wu! Welcome.</p>
Unless
Template Example
{{#unless license}}
<p class="warning">WARNING: This entry does not have a license!</p>
{{/unless}}
Test Data
{}
Rendering Result
<!-- Rendering Result -->
<p class="warning">WARNING: This entry does not have a license!</p>
Comparison Operators
Comparison operators support two usage modes: basic and with else.
The supported syntax is shown in the following table:
Expression | Explanation |
---|---|
greaterThan | > |
lessThan | < |
equals | = |
greaterThanOrEquals | >= |
lessThanOrEquals | <= |
notEquals | != |
basic
Template Example
<p>
{{#greaterThan truckWeight limitWeight}}
The truck is not allowed to pass.
{{/greaterThan}}
</p>
Test Data
{
"truckWeight": 6,
"limitWeight": 5
}
Rendering Result
<p>
The truck is not allowed to pass.
</p>
with else
Template Example
<p>
{{#greaterThan truckWeight limitWeight}}
The truck is not allowed to pass.
{{else}}
The truck is allowed to pass.
{{/greaterThan}}
</p>
Test Data
{
"truckWeight": 5,
"limitWeight": 5
}
Rendering Result
<p>
The truck is allowed to pass.
</p>
in, notIn
Like comparison operators, they also support two usage modes: basic and with else.
Template Example
<p>
{{#in fruit favoriteFruits}}
Like to eat.
{{else}}
Do not like to eat.
{{/in}}
</p>
Test Data
// Test Data One
{
"fruit": "Apple",
"favoriteFruits": [
"Apple",
"Banana",
"Orange"
]
}
// Test Data二
{
"fruit": "Grape",
"favoriteFruits": [
"Apple",
"Banana",
"Orange"
]
}
Rendering Result
<!-- Rendering Result One -->
<p>
Like to eat.
</p>
<!-- Rendering Result Two -->
<p>
Do not like to eat.
</p>
Iteration
Each
Template Example
<ul>
{{#each fruits}}
<li>{{this}}</li>
{{/each}}
</ul>
Test Data
{
"fruits": [
"Apple",
"Banana",
"Orange"
]
}
Rendering Result
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
Supplementary Explanation
When using each
to iterate over an array, you can use {{@index}}
to reference the current loop index.
{{#each array}} {{@index}}: {{this}} {{/each}}
In addition, for object iteration, {{@key}}
is used to reference the current key name.
{{#each object}} {{@key}}: {{this}} {{/each}}
With
Template Example
{{#with user.profile}}
<p>Hello {{firstName}}</p>
{{/with}}
Test Data
{
"user": {
"profile": {
"firstName": "Engagelab"
}
}
}
Rendering Result
<p>Hello Engagelab</p>