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.
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.
With access to our Design Library full of pre-built email templates, you're free to spend time crafting content that connects.
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
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 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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With