【问题标题】:Mandrill — How to format complex data for handlebars templatesMandrill — 如何格式化车把模板的复杂数据
【发布时间】:2018-10-15 17:39:16
【问题描述】:

我正在尝试通过 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 作为其模板语言。

我错过了什么?

【问题讨论】:

    标签: templates mandrill


    【解决方案1】:

    经过反复试验,我已经解决了这个问题。原来只有 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>
    

    工作正常。

    文档在这方面有点误导。

    【讨论】:

    • 你打败了我的答案。 :) 每个合并标签都需要采用这种格式,但内容可以是 JSON 对象。
    猜你喜欢
    • 2018-02-23
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多