Felix13th
Felix13th

Reputation: 1

Advanced Conditional Logic in Sendgrid Dynamic Templates

We are looking to populate information based on the data sent to SendGrid using dynamic content in dynamic templates.

We are sending transactional data to SendGrid to populate information in the dynamic template and I am looking to configure a rule as follows:

I was not able to figure out the code for the above scenarios if that is possible at all?

In a related scenario I am trying to do the same but with equals:

I know there is the possibility to configure dynamic content generation in Sendgrid templates using Handlebars. For example: https://docs.sendgrid.com/for-developers/sending-email/using-handlebars#conditional-statements

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

Unfortunately, I was not able to make the above code work, as it seems too inflexible.

Upvotes: 0

Views: 2360

Answers (1)

Swimburger
Swimburger

Reputation: 7164

TLDR: Instead of doing those conditionals and manipulations inside of your Handlebar template, you'll need to pre-calculate those things and pass them in as boolean properties or strings to make it usable in Handlebars.


Having said that, I experimented with the SendGrid templating and I could achieve some of the things you asked for, however, the code is ugly/hacky.

{{#equals RateType.[0] "A" }}
ABC
{{/equals}}
{{#equals RateType.[0] "B" }}
DEF
{{/equals}}
{{#equals RateType.[0] "C"}}
GHI
{{/equals}}
{{#equals RateType.[0] "D"}}
GHI
{{/equals}}
{{#equals RateType.[0] "E"}}
GHI
{{/equals}}

{{#notEquals RateType.[0] "A"}}
{{#notEquals RateType.[0] "B"}}
{{#notEquals RateType.[0] "C"}}
{{#notEquals RateType.[0] "D"}}
{{#notEquals RateType.[0] "E"}}
JKL
{{/notEquals}}
{{/notEquals}}
{{/notEquals}}
{{/notEquals}}
{{/notEquals}}

By grabbing the first letter using [0] I could check what letter the RateType starts with. For some reason I'm not able to grab the rest of the letters using [1] [2] etc.

If you could grab multiple letters like that, you should be able to do something like this, even more hacky:

{{#equals TransactionNumber.[0] "A" }}
{{#equals TransactionNumber.[1] "B" }}
{{#equals TransactionNumber.[2] "C" }}
ABC
{{/equals}}
{{/equals}}
{{/equals}}

{{#equals TransactionNumber.[0] "4" }}
{{#equals TransactionNumber.[1] "5" }}
{{#equals TransactionNumber.[2] "6" }}
ABC
{{/equals}}
{{/equals}}
{{/equals}}


{{#equals TransactionNumber.[0] "7" }}
{{#equals TransactionNumber.[1] "8" }}
{{#equals TransactionNumber.[2] "9" }}
DEF
{{/equals}}
{{/equals}}
{{/equals}}

{{#equals TransactionNumber.[0] "0" }}
{{#equals TransactionNumber.[1] "0" }}
{{#equals TransactionNumber.[2] "0" }}
DEF
{{/equals}}
{{/equals}}
{{/equals}} 

To be extra clear and avoid confusion, the above sample doesn't work.

So the only way to do this is to pre-calculate some of these things and pass the calculated results to the template which you then use in the template.

Upvotes: 0

Related Questions