我如何使用Mandrill模板在MERGE VARS中传递的数组外呈现列表 [英] How would I render a list with Mandrill template out of array passed in MERGE VARS

查看:86
本文介绍了我如何使用Mandrill模板在MERGE VARS中传递的数组外呈现列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须替换为 merge_vars .

{
   "key":"some key",
   "template_name":"order-confirmation",
   "template_content":[
      {
         "name":"ORDERNUMBER",
         "content":"12312312321"
      },
      {
         "name":"PRICE",
         "content":"35.10"
      },
      {
         "name":"NAME",
         "content":"Some name"
      },
      {
         "name":"PICKUPDATE",
         "content":"2013-05-10"
      },
      {
         "name":"ORDERITEMS",
         "content":[
            {
               "NUMPRODUCTS":"26",
               "PRODUCTNAME":"Milk",
               "PRODUCTPRICE":"1.35 EUR"
            }
         ]
      },
      {
         "name":"SERVICENUMBER",
         "content":"12345"
      },
      {
         "name":"PICKUPPOINT",
         "content":"AAA 350"
      },
      "message":{
         "from_email":"support@support.com",
         "to":[
            {
               "email":"asdasd@asda.com"
            }
         ],
         "subject":"Subject text",
         "attachments":[

         ]
      },
      "async":false
   }
}

我应该如何制作html占位符?我是这样做的,但是不起作用.我只对 ORDERITEMS 感兴趣.

how should I make html placeholders? I did it like this but it doesn't work. I'm only interested in ORDERITEMS.

<tr>
 <td>*|ORDERITEMS:NUMPRODUCTS|*</td>
 <td>*|ORDERITEMS:PRODUCTNAME|*</td>
 <td>*|ORDERITEMS:PRODUCTPRICE|*</td>
</tr>

推荐答案

据我所知,使用Mandrill中的普通模板引擎, content 属性唯一受支持的类型是字符串,因此无法提供结构.

As far as I know, using the normal templating engine in Mandrill, the only supported type for the content attribute is string, so there is no way to provide a structure.

最近,(2015年1月13日)Mandrill宣布支持车把模板,并因此有可能在 content 中包含数组.

Recently, (Jan 13 2015) Mandrill announced support for handlebars templates, and with that, the posibility to include arrays in the content.

http://blog.mandrill.com/handlebars-for-templates-and-dynamic-content.html

使用这种新的模板语言,不仅可以根据需要访问子属性,而且甚至可以在数组内循环,甚至嵌套do循环.(请参阅博客上的此评论帖子)

With this new template language it is possible to not only access sub-propertys as you need, but even loop inside arrays, and even nested do loops. (see this comment on the blog post)

这是从博客文章中提取的一个循环示例:

This is an example extracted from the blog post for doing a loop:

合并变量或模板内容:

{
    "name": "products",
    "content": [
        {
            "img": "http://kbcdn.mandrill.com/nesting-penguin.png",
            "qty": 2,
            "sku": "PENG001",
            "name": "Penguin",
            "description": "Solid wood, hand-painted penguin nesting doll with 5 different sizes included. Limited Edition.",
            "price": "12.99",
            "ordPrice": "25.98"
        },
        {
            "img": "http://kbcdn.mandrill.com/nesting-bear.png",
            "qty": 3,
            "sku": "BBEAR001",
            "name": "Brown bear",
            "description": "Solid wood, hand-painted brown bear nesting doll. Coordinates with our entire Bear collection. Includes 6 nested sizes.",
            "price": "12.99",
            "ordPrice": "38.97"
        }
    ]
}

这是在模板中使用它的方法:

And this is how to consume it in the template:

{{#each products}}
<tr class="item">
    <td valign="top" class="textContent">
        <img src="{{img}}" width="50" height="75" class="itemImage" />
        <h4 class="itemName">{{name}}</h4>
        <span class="contentSecondary">Qty: {{qty}} x ${{price}}/each</span><br />
        <span class="contentSecondary sku"><em>{{sku}}</em></span><br />
        <span class="contentSecondary itemDescription">{{description}}</span>
    </td>
    <td valign="top" class="textContent alignRight priceWidth">
        ${{ordPrice}}
    </td>
</tr>
{{/each}}

您可以这样做:

{{#each ORDERITEMS}}
<tr>
 <td>{{NUMPRODUCTS}}*</td>
 <td>{{PRODUCTNAME}}</td>
 <td>{{PRODUCTPRICE}}</td>
</tr>
{{/each}}

这篇关于我如何使用Mandrill模板在MERGE VARS中传递的数组外呈现列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆