Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mandrill API - Handlebar each loop in Table

I use the Mandrill API to send my transactional Mails via PHP.

Now I'm running in the problem, that when I try to loop through multiple vars only the last one is shown.

this is my variable for global_merge_vars

array(
        array(
            'name' => 'products',
            'content' => array(
                array(
                    "name" => "Product 1",
                    "price" => "65€"
                ),
                array(
                    "name" => "Product 2",
                    "price" => "65€"
                ),
                array(
                    "name" => "Product 3",
                    "price" => "65€"
                )
            )               
        )
    );

My issue is glued to the products part with the array as content.

So if i try the following:

{{#each products}}
  {{name}} - {{price}}<br>
{{/each}}

I get

Product 1 - 65€
Product 2 - 65€
Product 3 - 65€

So far so good...

but if I try to wrap the whole thing in a table, I always just get the last array element shown...

<table>
  {{#each products}}
    <tr>
      <td>{{name}} - {{price}}</td>
    </tr>
    {{/each}}
</table>

results in:

Product 3 - 65€

Actually, I think, it's just a stupid mistake on my side, but right now, I have no idea what is the problem!

So thank you all in advance for your help :)

___________________UPDATE________________________

i also found out, that it works if i put the whole table in the loop, like the following:

{{#each products}}
  <table>
    <tr>
      <td>{{name}} - {{price}}</td>
    </tr>
  </table>
{{/each}}

but thats not really what i want as stated before :)

like image 559
FalcoB Avatar asked Oct 04 '18 14:10

FalcoB


1 Answers

This is probably too late for OP but after spending a few hours trying to understand this, I came across the solution on a GitHub thread. Basically you put your handlebar tags on a HTML comment like this:

<table>
  <!-- {{#each products}} -->
  <tr>
    <td>{{name}} - {{price}}</td>
  </tr>
  <!-- {{/each}} -->
</table>

credits: gurpreetatwal

like image 69
Layon Ferreira Avatar answered Sep 23 '22 00:09

Layon Ferreira