Does SendGrid template engine have conditionals?

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 %}

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.


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


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.

Tags:

Sendgrid