Mandrill — 如何为车把模板设置复杂数据的格式
Mandrill — How to format complex data for handlebars templates
我正在尝试通过 Mandrill 发送模板化电子邮件,但在获取我发送的数据的模板时遇到问题。
The docs 说我需要将我的数据转换为 [{ name: 'propertyName', content: 'the content' }]
的数组
他们给出的例子如下
数据
"global_merge_vars": [
{
"name": "user_name",
"content": "Mandrill_User1"
}
]
模板
<p>Thanks for registering! Your username is {{user_name}}.</p>
结果
<p>Thanks for registering! Your username is Mandrill_User1.</p>
在我的例子中,数据更复杂。
我有类似的东西
{
"firstname": "Tyler",
"lastname": "Durden",
"fullname": "Tyler Durden",
"email": "tyler.durden@testy.tes",
"company": {
"name": "Company 1",
"role": {
"slug": "supplier",
"name": "Supplier"
}
}
}
我将其转换为 name
:content
对,如下所示,作为 global_merge_vars
发送
[
{ name: 'firstname', content: 'Tyler' },
{ name: 'lastname', content: 'Durden' },
{ name: 'fullname', content: 'Tyler Durden' },
{ name: 'email', content: 'tyler.durden@testy.tes' },
{
name: 'company',
content: [
{ name: 'name', content: 'Company 1' },
{
name: 'role',
content: [
{ name: 'slug', content: 'supplier' },
{ name: 'name', content: 'Supplier' }
]
}
]
}
]
我的模板是
主题
Dear {{user.firstname}} {{company.name}} has been approved.
正文
<html>
<body>
<p>Dear {{user.firstname}},</p>
<p>Your company {{company.name}} has been approved.</p>
</body>
</html>
但是结果是
主题
亲爱的已获批
正文
<html>
<body>
<p>Dear ,</p>
<p>Your company has been approved.</p>
</body>
</html>
我已将 Mandrill 设置为使用 handlebars
作为其模板语言。
我错过了什么?
经过反复试验,我解决了这个问题。结果只有 top level 对象需要变成 name
, content
对。低阶对象结构可以保留为普通 JSON 对象。
所以
{
"name": "user"
"content": {
"firstname": "Tyler",
"lastname": "Durden",
"fullname": "Tyler Durden",
"email": "tyler.durden@testy.tes",
"company": {
"name": "Company 1",
"role": {
"slug": "supplier",
"name": "Supplier"
}
}
}
}
模板主题:Hello {{user.firstname}}
和正文
<html>
<body>
<p>Dear {{user.firstname}},</p>
<p>Your company {{user.company.name}} has been approved.</p>
</body>
</html>
工作正常。
文档在这方面有点误导。
我正在尝试通过 Mandrill 发送模板化电子邮件,但在获取我发送的数据的模板时遇到问题。
The docs 说我需要将我的数据转换为 [{ name: 'propertyName', content: 'the content' }]
他们给出的例子如下
数据
"global_merge_vars": [
{
"name": "user_name",
"content": "Mandrill_User1"
}
]
模板
<p>Thanks for registering! Your username is {{user_name}}.</p>
结果
<p>Thanks for registering! Your username is Mandrill_User1.</p>
在我的例子中,数据更复杂。
我有类似的东西
{
"firstname": "Tyler",
"lastname": "Durden",
"fullname": "Tyler Durden",
"email": "tyler.durden@testy.tes",
"company": {
"name": "Company 1",
"role": {
"slug": "supplier",
"name": "Supplier"
}
}
}
我将其转换为 name
:content
对,如下所示,作为 global_merge_vars
[
{ name: 'firstname', content: 'Tyler' },
{ name: 'lastname', content: 'Durden' },
{ name: 'fullname', content: 'Tyler Durden' },
{ name: 'email', content: 'tyler.durden@testy.tes' },
{
name: 'company',
content: [
{ name: 'name', content: 'Company 1' },
{
name: 'role',
content: [
{ name: 'slug', content: 'supplier' },
{ name: 'name', content: 'Supplier' }
]
}
]
}
]
我的模板是
主题
Dear {{user.firstname}} {{company.name}} has been approved.
正文
<html>
<body>
<p>Dear {{user.firstname}},</p>
<p>Your company {{company.name}} has been approved.</p>
</body>
</html>
但是结果是
主题
亲爱的已获批
正文
<html>
<body>
<p>Dear ,</p>
<p>Your company has been approved.</p>
</body>
</html>
我已将 Mandrill 设置为使用 handlebars
作为其模板语言。
我错过了什么?
经过反复试验,我解决了这个问题。结果只有 top level 对象需要变成 name
, content
对。低阶对象结构可以保留为普通 JSON 对象。
所以
{
"name": "user"
"content": {
"firstname": "Tyler",
"lastname": "Durden",
"fullname": "Tyler Durden",
"email": "tyler.durden@testy.tes",
"company": {
"name": "Company 1",
"role": {
"slug": "supplier",
"name": "Supplier"
}
}
}
}
模板主题:Hello {{user.firstname}}
和正文
<html>
<body>
<p>Dear {{user.firstname}},</p>
<p>Your company {{user.company.name}} has been approved.</p>
</body>
</html>
工作正常。
文档在这方面有点误导。