12

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":"support@support.com",
         "to":[
            {
               "email":"asdasd@asda.com"
            }
         ],
         "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>
balexandre
  • 73,608
  • 45
  • 233
  • 342
theDon
  • 121
  • 1
  • 6
  • did you tried ***var api = new MandrillApi(ApiKey); var resultRender = api.Render(templateExample, templateContent, null);*** – Kiquenet Feb 14 '15 at 11:28

3 Answers3

13

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}}
David Lay
  • 2,956
  • 4
  • 27
  • 48
  • It's not working, when I sent the email, the loop was printed in the email. Template didn't execute that. Here's what my template looks like: ```{{#each notifications}}

    {{name}}

    {{/each}}```
    – Rohit Khatri Dec 29 '18 at 13:05
0

In case someone has these same problem, and the doc solution does not work, check the field merge_language in your message json. It has to be defined to handlebars.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 28 '21 at 23:20
0

10 years on and this question is still useful but here are some updates for those who might need it.

Handlebars is the way to go when trying to render a list in a mandrill / mailchimp email. The primary take away here is that to use handlebars you need to make sure you enable the "handlebars" scripting language, by default this is set to mailchimp.

In the API this is done by adding merge_language to the message dictionary:

merge_language: the merge tag language to use when evaluating merge
tags, either mailchimp or handlebars Possible values: "mailchimp" 
or "handlebars".

For example:


response = client.messages.send_template(
    {
        "template_name": "template_name",
        "template_content": [{}], 
        "message": {
            "merge_language": "handlebars",
        }
    }
)
Matt Seymour
  • 8,880
  • 7
  • 60
  • 101