Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I render a list with Mandrill template out of array passed in MERGE VARS

I have to make substitution with 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":"[email protected]",
         "to":[
            {
               "email":"[email protected]"
            }
         ],
         "subject":"Subject text",
         "attachments":[

         ]
      },
      "async":false
   }
}

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>
like image 655
theDon Avatar asked May 17 '13 09:05

theDon


1 Answers

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.

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

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:

The merge variable or template content:

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

In your case, you could do:

{{#each ORDERITEMS}}
<tr>
 <td>{{NUMPRODUCTS}}*</td>
 <td>{{PRODUCTNAME}}</td>
 <td>{{PRODUCTPRICE}}</td>
</tr>
{{/each}}
like image 88
David Lay Avatar answered Nov 18 '22 00:11

David Lay