邮件动态模板

最新更新:2024-12-02

Engagelab Email 动态模板支持 Handlebars 模板语言来呈现您通过 API 发送的个性化内容。

变量替换

基础替换

模板示例

<p>Hello {{firstname}}</p>
          <p>Hello {{firstname}}</p>

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

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

        
此代码块在浮窗中显示

嵌套对象替换

模板示例

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

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

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

        
此代码块在浮窗中显示

formatDate

formatDate 接收 Unix 时间戳或 ISO8601 格式的时间,并使用下表中的标记将其转换为您指定的格式。 以 UTC 时间 1987-09-20T20:55:00.000Z 为例, 转换展示结果如下表:

占位符 展示结果
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

模板示例

<p>{{formatDate timeStamp dateFormat}}</p> <!-- 时区偏移示例 --> <p>{{formatDate timeStamp dateFormat timezoneOffset}}</p>
          <p>{{formatDate timeStamp dateFormat}}</p>
<!-- 时区偏移示例 -->
<p>{{formatDate timeStamp dateFormat timezoneOffset}}</p>

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

<!-- 渲染结果(模板一) --> <p>1987-09-20 12:55:00 PM</p> <!-- 渲染结果(模板二) --> <p>1987-09-20 20:55:00 PM</p>
          <!-- 渲染结果(模板一) -->
<p>1987-09-20 12:55:00 PM</p>

<!-- 渲染结果(模板二) -->
<p>1987-09-20 20:55:00 PM</p>

        
此代码块在浮窗中显示

Insert

模板示例

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

        
此代码块在浮窗中显示

测试数据

// 测试数据一 {} // 测试数据一 { "businessName": "Engagelab" }
          // 测试数据一
{}

// 测试数据一
{
    "businessName": "Engagelab"
}

        
此代码块在浮窗中显示

渲染结果

<!-- 渲染结果一 --> <p>Hello Ben! Thank you for contacting us about your business.</p> <!-- 渲染结果二 --> <p>Hello Customer! Thank you for contacting us about Engagelab.</p>
          <!-- 渲染结果一 -->
<p>Hello Ben! Thank you for contacting us about your business.</p>

<!-- 渲染结果二 -->
<p>Hello Customer! Thank you for contacting us about Engagelab.</p>

        
此代码块在浮窗中显示

条件语句

If, Else, Else 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}}
          {{#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}}

        
此代码块在浮窗中显示

测试数据

// 测试数据一 { "user": { "profile": { "firstName": "Zhang San", "isTeacher": true } } } // 测试数据二 { "user": { "profile": { "firstName": "Li Si", "isStudent": true } } } // 测试数据三 { "user": { "profile": { "firstName": "Wang Wu", } } }
          // 测试数据一
{
    "user": {
        "profile": {
            "firstName": "Zhang San",
            "isTeacher": true
        }
    }
}

// 测试数据二
{
    "user": {
        "profile": {
            "firstName": "Li Si",
            "isStudent": true
        }
    }
}

// 测试数据三
{
    "user": {
        "profile": {
            "firstName": "Wang Wu",
        }
    }
}

        
此代码块在浮窗中显示

渲染结果

<!-- 渲染结果(测试数据一) --> <p>Hello, Teacher Zhang San</p> <!-- 渲染结果(测试数据二) --> <p>Hello, Student Li Si</p> <!-- 渲染结果(测试数据三) --> <p>Hello, Wang Wu! Welcome.</p>
          <!-- 渲染结果(测试数据一) -->
<p>Hello, Teacher Zhang San</p>

<!-- 渲染结果(测试数据二) -->
<p>Hello, Student Li Si</p>

<!-- 渲染结果(测试数据三) -->
<p>Hello, Wang Wu! Welcome.</p>

        
此代码块在浮窗中显示

Unless

模板示例

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

        
此代码块在浮窗中显示

测试数据

{}
          {}

        
此代码块在浮窗中显示

渲染结果

<!-- 渲染结果 --> <p class="warning">WARNING: This entry does not have a license!</p>
          <!-- 渲染结果 -->
<p class="warning">WARNING: This entry does not have a license!</p>

        
此代码块在浮窗中显示

比较运算符

比较运算符支持两种使用方式:basicwith else

支持的语法如下表所示:

表达式 说明
greaterThan >
lessThan <
equals =
greaterThanOrEquals >=
lessThanOrEquals <=
notEquals !=

basic

模板示例

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

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

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

        
此代码块在浮窗中显示

with else

模板示例

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

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

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

        
此代码块在浮窗中显示

in, notIn

与比较运算符一样支持两种使用方式:basicwith else

模板示例

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

        
此代码块在浮窗中显示

测试数据

// 测试数据一 { "fruit": "Apple", "favoriteFruits": [ "Apple", "Banana", "Orange" ] } // 测试数据二 { "fruit": "Grape", "favoriteFruits": [ "Apple", "Banana", "Orange" ] }
          // 测试数据一
{
    "fruit": "Apple",
    "favoriteFruits": [
        "Apple",
        "Banana",
        "Orange"
    ]
}

// 测试数据二
{
    "fruit": "Grape",
    "favoriteFruits": [
        "Apple",
        "Banana",
        "Orange"
    ]
}

        
此代码块在浮窗中显示

渲染结果

<!-- 渲染结果一 --> <p> Like to eat. </p> <!-- 渲染结果二 --> <p> Do not like to eat. </p>
          <!-- 渲染结果一 -->
<p>
Like to eat.
</p>

<!-- 渲染结果二 -->
<p>
Do not like to eat.
</p>

        
此代码块在浮窗中显示

迭代

循环(Each)

模板示例

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

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

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

        
此代码块在浮窗中显示

补充说明

当使用 each 遍历数组时,可以使用 {{@index}} 引用当前循环索引

{{#each array}} {{@index}}: {{this}} {{/each}}
          {{#each array}} {{@index}}: {{this}} {{/each}}

        
此代码块在浮窗中显示

此外,对于对象迭代,{{@key}} 引用当前键名

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

        
此代码块在浮窗中显示

With 语法

模板示例

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

        
此代码块在浮窗中显示

测试数据

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

        
此代码块在浮窗中显示

渲染结果

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

        
此代码块在浮窗中显示
在文档中心打开
icon
联系销售