Dynamic Templates

Last updated:2024-12-02

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>
          <p>Hello {{firstname}}</p>

        
This code block in the floating window

Test Data

{ "firstName": "Engagelab" }
          { "firstName": "Engagelab" }

        
This code block in the floating window

Rendering Result

<p>Hello Engagelab</p>
          <p>Hello Engagelab</p>

        
This code block in the floating window

Nested Object Replacement

Template Example

<p>Hello {{user.profile.firstName}}</p>
          <p>Hello {{user.profile.firstName}}</p>

        
This code block in the floating window

Test Data

{ "user": { "profile": { "firstName": "Engagelab" } } }
          {
    "user": {
        "profile": {
            "firstName": "Engagelab" 
        }
    }
}

        
This code block in the floating window

Rendering Result

<p>Hello Engagelab</p>
          <p>Hello Engagelab</p>

        
This code block in the floating window

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>
          <p>{{formatDate timeStamp dateFormat}}</p>
<!-- Example of time zone offset -->
<p>{{formatDate timeStamp dateFormat timezoneOffset}}</p>

        
This code block in the floating window

Test Data

{ "timeStamp": "1987-09-20T12:55:00.000Z", "dateFormat": "YYYY-MM-DD HH:mm:ss A", "timezoneOffset": "+0800" }
          {
  "timeStamp": "1987-09-20T12:55:00.000Z",
  "dateFormat": "YYYY-MM-DD HH:mm:ss A",
  "timezoneOffset": "+0800"
}

        
This code block in the floating window

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>
          <!-- 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>

        
This code block in the floating window

Insert

Template Example

<p>Thank you for contacting us about {{insert businessName "your business"}}.</p>
          <p>Thank you for contacting us about {{insert businessName "your business"}}.</p>

        
This code block in the floating window

Test Data

// Test Data One {} // Test Data One { "businessName": "Engagelab" }
          // Test Data One
{}

// Test Data One
{
    "businessName": "Engagelab"
}

        
This code block in the floating window

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>
          <!-- 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>

        
This code block in the floating window

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}}
          {{#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}}

        
This code block in the floating window

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", } } }
          // 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",
        }
    }
}

        
This code block in the floating window

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>
          <!-- 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>

        
This code block in the floating window

Unless

Template Example

{{#unless license}} <p class="warning">WARNING: This entry does not have a license!</p> {{/unless}}
          {{#unless license}}
    <p class="warning">WARNING: This entry does not have a license!</p>
{{/unless}}

        
This code block in the floating window

Test Data

{}
          {}

        
This code block in the floating window

Rendering Result

<!-- Rendering Result --> <p class="warning">WARNING: This entry does not have a license!</p>
          <!-- Rendering Result -->
<p class="warning">WARNING: This entry does not have a license!</p>

        
This code block in the floating window

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>
          <p> 
{{#greaterThan truckWeight limitWeight}}
The truck is not allowed to pass.
{{/greaterThan}}
</p>

        
This code block in the floating window

Test Data

{ "truckWeight": 6, "limitWeight": 5 }
          {
    "truckWeight": 6,
    "limitWeight": 5
}

        
This code block in the floating window

Rendering Result

<p> The truck is not allowed to pass. </p>
          <p> 
The truck is not allowed to pass.
</p>

        
This code block in the floating window

with else

Template Example

<p> {{#greaterThan truckWeight limitWeight}} The truck is not allowed to pass. {{else}} The truck is allowed to pass. {{/greaterThan}} </p>
          <p> 
{{#greaterThan truckWeight limitWeight}}
The truck is not allowed to pass.
{{else}}
The truck is allowed to pass.
{{/greaterThan}}
</p>

        
This code block in the floating window

Test Data

{ "truckWeight": 5, "limitWeight": 5 }
          {
    "truckWeight": 5,
    "limitWeight": 5
}

        
This code block in the floating window

Rendering Result

<p> The truck is allowed to pass. </p>
          <p> 
The truck is allowed to pass.
</p>

        
This code block in the floating window

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>
          <p>
{{#in fruit favoriteFruits}}
Like to eat.
{{else}}
Do not like to eat.
{{/in}}
</p>

        
This code block in the floating window

Test Data

// Test Data One { "fruit": "Apple", "favoriteFruits": [ "Apple", "Banana", "Orange" ] } // Test Data二 { "fruit": "Grape", "favoriteFruits": [ "Apple", "Banana", "Orange" ] }
          // Test Data One
{
    "fruit": "Apple",
    "favoriteFruits": [
        "Apple",
        "Banana",
        "Orange"
    ]
}

// Test Data二
{
    "fruit": "Grape",
    "favoriteFruits": [
        "Apple",
        "Banana",
        "Orange"
    ]
}

        
This code block in the floating window

Rendering Result

<!-- Rendering Result One --> <p> Like to eat. </p> <!-- Rendering Result Two --> <p> Do not like to eat. </p>
          <!-- Rendering Result One -->
<p>
Like to eat.
</p>

<!-- Rendering Result Two -->
<p>
Do not like to eat.
</p>

        
This code block in the floating window

Iteration

Each

Template Example

<ul> {{#each fruits}} <li>{{this}}</li> {{/each}} </ul>
          <ul>
{{#each fruits}}
    <li>{{this}}</li>
{{/each}}
</ul>

        
This code block in the floating window

Test Data

{ "fruits": [ "Apple", "Banana", "Orange" ] }
          {
    "fruits": [
        "Apple",
        "Banana",
        "Orange"
    ]
}

        
This code block in the floating window

Rendering Result

<ul> <li>Apple</li> <li>Banana</li> <li>Orange</li> </ul>
          <ul>
    <li>Apple</li>
    <li>Banana</li>
    <li>Orange</li>
</ul>

        
This code block in the floating window

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}}
          {{#each array}} {{@index}}: {{this}} {{/each}}

        
This code block in the floating window

In addition, for object iteration, {{@key}} is used to reference the current key name.

{{#each object}} {{@key}}: {{this}} {{/each}}
          {{#each object}} {{@key}}: {{this}} {{/each}}

        
This code block in the floating window

With

Template Example

{{#with user.profile}} <p>Hello {{firstName}}</p> {{/with}}
          {{#with user.profile}}
<p>Hello {{firstName}}</p>
{{/with}}

        
This code block in the floating window

Test Data

{ "user": { "profile": { "firstName": "Engagelab" } } }
          {
    "user": {
        "profile": {
            "firstName": "Engagelab" 
        }
    }
}

        
This code block in the floating window

Rendering Result

<p>Hello Engagelab</p>
          <p>Hello Engagelab</p>

        
This code block in the floating window
在文档中心打开
icon
Contact Sales