Search code examples
mailchimpmandrill

Printing multidimensional arrays with Mandrill handlebars


All questions I can find pertaining to this topic are >4 years old, and no longer seem to be relevant. Such as How would I render a list with Mandrill template out of array passed in MERGE VARS

How can I print multidimensional arrays in a Mandrill template using handlebars?

Consider the following data structure

"global_merge_vars": [
    {
        "name": "items",
        "content": [
            {
                "ID": 5009,
                "Qty": 2,
                "Name": "Firefly Candle - Green Bamboo"
            }
            {
                "ID": 3544,
                "Qty": 1,
                "Name": "Aeropress Coffee Maker"
            }
        ]
    }
]

Then, in my template I have

<table>
    {{#each items}}
    <tr>
        <td><img src="https://example.com/imgs/{{ID}}.jpg" /></td>
        <td>{{Qty}}x {{Name}}</td>
    </tr>
    {{/each}}
</table>

And it always comes though empty. No content at all. I've tried {{#each items.content}}. I've tried {{this.Qty}}. I've tried every combination and every permutation thereof I can find online, but none of it seems to work.


Solution

  • The solution did end being {{#each items}} AND {{this.Qty}}. Thus making the final template look like:

    <table>
        {{#each items}}
        <tr>
            <td><img src="https://example.com/imgs/{{this.ID}}.jpg" /></td>
            <td>{{this.Qty}}x {{this.Name}}</td>
        </tr>
        {{/each}}
    </table>