郵件动态模板
最新更新: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>
此代碼塊在浮窗中顯示
比較運算符
比較運算符支援兩種使用方式:basic 和 with 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
與比較運算符一樣支援兩種使用方式:basic 和 with 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>
此代碼塊在浮窗中顯示