27

Sending transactional apis through SendGrid. My template (ported over from Mailchimp) has conditionals (e.g.

*|IF:SHOWTHISSECTION|*

in Mailchimp syntax). This includes or excludes sections of the template based on a variable.

I can't find the analog in SendGrid, does it simply not have this capability? I'd like to suppress certain sections depending on the presence/absence of a substitution variable.

Mark Watkins
  • 862
  • 1
  • 9
  • 19

7 Answers7

35

SendGrid supports this natively now:

{{#if user.profile.male}}
  <p>Dear Sir</p>
{{else if user.profile.female}}
  <p>Dear Madame</p>
{{else}}
  <p> Dear Customer</p>
{{/if}}

Reference: https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements

DauleDK
  • 3,313
  • 11
  • 55
  • 98
rothschild86
  • 1,433
  • 16
  • 22
  • Do you know if can do something like `{{#if count > 3}}` ? – DauleDK Aug 16 '19 at 09:41
  • 1
    @DauleDK: Did you find the solution for that? – 0xh8h Sep 10 '19 at 12:55
  • Hi @HoangTrinh - I could not make it work. I "fixed" my issue by providing more booleans like `moreThanOne` :) – DauleDK Sep 10 '19 at 17:36
  • 1
    @DauleDK: me too. I need to set the status color based on the status. So I "fixed" it by adding the "color" variable from Javascript, and set the color directly without using the if statement :)) – 0xh8h Sep 11 '19 at 02:42
  • 1
    you can use the `#greaterThan` block. Checkout [here](https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#greaterthan) – Prabhat Choudhary Apr 29 '21 at 15:08
  • Works fine but the problem is that it adds extra space on lines where you added your conditional statements – Ferdossi Sep 14 '22 at 17:48
6

It's a horrible hack, but by introducing new variables and using CSS, you can hide the relevant portions of mails using display. So where before in Mandrill/MailChimp I'd have something like:

    *|IF:FAKEVAR|* 
    <p>Show some text here</p>
    *|END:IF|*

Instead, introduce a new variable IF_FAKEVAR, whose value is either "none" or "inherit" depending on whether FAKEVAR has a value, then do this:

<p style="display: *|IF_FAKEVAR|*">Show some text here</p>

While it's a hack, for very complex email templates, it avoids sending 70k bytes to the server for every single email, which when you have thousands or tens of thousands of mails, is prohibitive.

Mark Watkins
  • 862
  • 1
  • 9
  • 19
  • 1
    btw it appears you have to do style="display: *|IF_FAKEVAR|* !important;" if you really want gmail to do it...sigh. – Mark Watkins Oct 02 '15 at 18:56
  • This is really the only option unless your sections don't contain much HTML, otherwise all your HTML ends up in your application instead of the send grid template. – Jim Edelstein Sep 09 '16 at 18:58
3

SendGrid templating does not support this, but you can use a templating API like sendwithus to accomplish this on top of your SendGrid account. I believe sendwithus supports jinja conditionals, so you could do the following:

{% if variable %}
    <h1>{{ variable }}</h1>
{% endif %}
bvanvugt
  • 1,212
  • 1
  • 8
  • 15
  • Thank you. SendWithUs would work although that's a pricey solution and introduces yet another templating engine to my mix.. Hopefully sendgrid will get around to adding this.... – Mark Watkins Jun 07 '15 at 14:12
3

Sendgrid supports conditional using Handlebar

{{#if user.profile.male}}
  <p>Dear Sir</p>
{{else if user.profile.female}}
  <p>Dear Madame</p>
{{else}}
  <p> Dear Customer</p>
{{/if}}

from their documentation here https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements

kheengz
  • 840
  • 12
  • 10
3

Below handlebars can be used in Sendgrid dynamic templates:

Conditional statements:
{{#if variable}}
{{#unless variable}}
{{#greaterThan variable value}}
{{#lessThan variable value}}
{{#equals variable value}}
{{#notEquals variable value}}
{{#and variable1 variable2}}
{{#or variable1 variable2}}

Looping statements:
{{#each hash}}

Refer https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/ for detailed information

Aravind K
  • 31
  • 1
2

SendGrid doesn't have true conditionals, but it does have Section Tags. With those, you can define a block of text at the message level (as opposed to the distinct recipient level of a Substitution Tag), and then call the appropriate section for the recipient as needed.

jacobmovingfwd
  • 2,185
  • 14
  • 11
2

I Know this is old, but I had the same problem and I found a solution compatible with several email managers that maybe it's helpful for someone.

You can use substitution tags with the html comment symbols value in case you want to hide a section.

{%OPEN_COMMENT}
<h1>Whatever section you want to hide</h1>
{%CLOSE_COMMENT}

Replace tags with "" respectively if you want to hide the section. Replace them with empty strings in the other case.

Paco
  • 21
  • 1