Search code examples
sendgrid-templates

Sendgrid handlebars #if statement in html checkbox


is it possible to do a replacement inside a HTML tag?

Template:

<input type="checkbox" disabled {{#if CompleteAndTrueConfirmation}}checked{{/if}} >

Test Data:

{
    "CompleteAndTrueConfirmation": true
}

I get the error:

Unexpected character after / in tag. Expected >.

enter image description here

This template works but looks cumbersome:

{{#if CompleteAndTrueConfirmation}}
<input type="checkbox" disabled checked />
{{else}}
<input type="checkbox" disabled />
{{/if}}

Solution

  • No, it's not possible to use a Handlebars block like {{#if ... within an HTML start or end tag because it interferes with the HTML parsing in the SendGrid template editor. ☹

    More info:

    When I edit module HTML, paste your template, and save, I get an error: enter image description here

    When I edit the code again, I see the code has been modified because the editor tries to fix the HTML by making attributes out of the bits of Handlebars code: enter image description here

    It is possible to do substitution where the test data contains HTML (example), but that kind of defeats the purpose of using a template.