Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SendGrid template engine have conditionals?

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.

like image 936
Mark Watkins Avatar asked Jun 06 '15 15:06

Mark Watkins


People also ask

What is SendGrid dynamic template?

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.

Does SendGrid have email templates?

With access to our Design Library full of pre-built email templates, you're free to spend time crafting content that connects.


3 Answers

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

like image 70
rothschild86 Avatar answered Sep 22 '22 16:09

rothschild86


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.

like image 37
Mark Watkins Avatar answered Sep 21 '22 16:09

Mark Watkins


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 %}
like image 22
bvanvugt Avatar answered Sep 23 '22 16:09

bvanvugt