Search code examples
twiliosendgridsendgrid-api-v3

Conditional logic in Sendgrid Dynamic templates


Can I configure the Sendgrid dynamic template one of these options based on user behavior?

  • One or more URL's to click
  • Redirect to a form

Does SendGrid template support conditional logic or is there an alternative way to achieve this via dynamic templates? ( I am looking for an AMP alternative actually )


Solution

  • You can send different content to a user based on the dynamic template data using handlebars conditionals. But that still appears as static content in the email. For example:

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

    If you want the contents of the email to change dynamically within the email like an AMP email, but without using AMP, then you can use CSS to make a dynamic reveal. This isn't part of SendGrid Dynamic Templates though, it is just built with the HTML and CSS that goes into the email (which you can build in SendGrid, it's just not specific to SendGrid). This CSS technique may not be supported in all email clients though, so be sure to build the email in such a way that it can work without this behaviour too.