Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Sendgrid Design template, how to use handlebar iteration for table?

I'm using SendGrid online "Design" template, with a module "code".

In their documentation (https://sendgrid.com/docs/ui/sending-email/editor/#code-modules), they say that de code editor does not modify or validate any HTML.

If I write this piece of code inside the code module:

<ul>
{{#each items}}
    <li>test</li>
{{/each}}
</ul>
<table>
<tbody>
{{#each items}}
    <tr>
        <td>Col 1</td>
        <td>Col 2</td>
    </tr>
{{/each}}
</tbody>
</table>

it results in:

<ul>
{{#each items}}
    <li>test</li>
{{/each}}
</ul>
{{#each items}}{{/each}}
<table>
<tbody><tr>
        <td>Col 1</td>
        <td>Col 2</td>
    </tr></tbody>
</table>

We can see that the {{each}} function stays in the right place for the ul, but is remove from inside of the table. Is this a temporary bug? How can I do this simple operation?

Thanks for your help

like image 931
rekam Avatar asked Dec 04 '18 13:12

rekam


People also ask

What is handlebars in SendGrid?

Handlebars syntax allows you to personalize the messages in your templates by inserting customers' names and other data to make an email relevant to each individual recipient.

What is a dynamic template in SendGrid?

Sendgrid dynamic template uses handlebarjs syntax to replace variables with values in HTML. The template contains variables with double curly braces. Below code is converted to html by replacing name with actual value in plain text.


2 Answers

I found a undocumented way to make this works. You will need to comment out the each helper like this:

<table>
<tbody>
<!-- {{#each items}} -->
    <tr>
        <td>Col 1</td>
        <td>Col 2</td>
    </tr>
<!-- {{/each}} -->
</tbody>
</table>
like image 66
Tan Duong Avatar answered Oct 19 '22 08:10

Tan Duong


I get the same issue. Definitely a bug in the Design Editor. My work around was to:

  1. Style the email with the Design Editor.
  2. Export HTML.
  3. Go back and create a new version of the transaction email using the 'Code Editor' and not the 'Design Editor'.
  4. Paste in the previously exported HTML.
  5. Find the table that needs the {{each}} loop and place the functions exactly as you did.
like image 31
Craig Liesinger Avatar answered Oct 19 '22 07:10

Craig Liesinger