Sendgrid templates: Handlebars "equals" with numbers don't work
Some of the handlebars examples in sendgrid handlebars docs just simple don't work. Don't render anything, also without error or feedback.
{{#equals "1" 1}}
Are equals
{{/else}}
Are not equals
{{#equals}}
Expected content
Are equals
Actual content
''
https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#equals
Hi @joanmiralles
We're sorry you're having trouble with the handlebars helpers. Could you provide more of your template code to help us troubleshoot? The equals helper seems to be working for me.
From the code you've posted, I see a few things that may be causing your issue. First, the {{else}} should not include a slash, /, because it's not a closing tag. However, your closing equals should have a slash rather than a hash. This is what it would all look like:
{{#equals "1" 1}} Are equals {{else}} Are not equals {{/equals}}
We hope this helps,
Twilio SendGrid Docs Team
Hi @joanmiralles
We're sorry you're having trouble with the handlebars helpers. Could you provide more of your template code to help us troubleshoot? The
equalshelper seems to be working for me.From the code you've posted, I see a few things that may be causing your issue. First, the
{{else}}should not include a slash,/, because it's not a closing tag. However, your closingequalsshould have a slash rather than a hash. This is what it would all look like:
{{#equals "1" 1}} Are equals {{else}} Are not equals {{/equals}}We hope this helps,
Twilio SendGrid Docs Team
Sorry I made some mistakes rewriting the code instead of copy paste. But using your exact code I don't get any result either.
So:
{{#equals "1" 1}} Are equals {{else}} Are not equals {{/equals}}
does not show anything
and
{{#equals 1 1}} Are equals {{else}} Are not equals {{/equals}}
Displays 'Are equals'.
I'm using Design mode if that helps.
Hi @joanmiralles
Thank you for clarifying. I'm still not seeing that behavior on my side. I pasted your examples into text blocks using the Design editor and got "Are equals" in both cases. I've added a screenshot of my Design and what arrived to my inbox in case it's helpful when troubleshooting.
Design editor with handlebars code
Handlebars rendered in received email
Feel free to pass more details if there is missing information in the docs. You can also open a support ticket for help with your individual setup. You can access support contact options by logging into https://support.sendgrid.com.
Best, Twilio SendGrid Docs Team
I am also having problems with equals, very similar to the ones joanmiralles mentioned!
The misunderstanding was that the problem is with rendering in the dynamic template editor view not the emails that are sent out. Emails are sent with the correct information, but it is extremely confusing while editing a template!
Using the example from the docs shows that the comparison between strings and numericals & rendering elements in the else branch are both broken in the template editor realtime preview.
In a fresh dynamic template I put:
<html>
<body>
{{#equals "1" 1}} Are equals {{else}} Are not equals {{/equals}}
</body>
</html>
Nothing is rendered on the righthand side.
When I remove the quotes to instead compare {{#equals 1 1}} it correctly renders "Are equals", but if I change it to {{#equals 1 2}} nothing is rendered when I would expect it to render "Are not equals". From what I can tell the elements in the else branch are never rendered in the template example email view.
@astuteape could you see if you can reproduce this? Tagging you in case you don't get notified for responses to closed issues 😄 Thanks!
Hi @eywolfe
Thank you for bringing this to my attention. I am getting the same behavior. I'll reopen this issue and bring it up with the product teams. For now, I'll make a note in the docs about the preview not checking those values correctly.
I have a similar issue with variable inside the equals:
{{#equals value1 value2}}
Equals {{value1}}
{{else}}
Not Equals {{value2}}
{{/equals}}
I can see the text Equals and Not Equals displayed based on the values of value1 and value2 however, {{value1}} and {{value2}} are not expanded and their values are not displayed.
Moving Equals {{value1}} outside of the equals statement works as expected and {{value1}} is properly expanded
I have a similar issue also.
The variables inside equals tag appears when I send an email but values doesn't displayed on preview mode.
It needs to be fixed because preview mode with dynamic data provide us to see what kind of email we will send before we sent but in this case what we see and send are not equal.
I have a similar issue also.
The variables inside equals tag appears when I send an email but values doesn't displayed on preview mode.
It needs to be fixed because preview mode with dynamic data provide us to see what kind of email we will send before we sent but in this case what we see and send are not equal.
Yes! I wasted hours trying to debug my view in the preview window until I found this issue and your comment!
I have a similar issue with variable inside the equals:
{{#equals value1 value2}} Equals {{value1}} {{else}} Not Equals {{value2}} {{/equals}}I can see the text Equals and Not Equals displayed based on the values of value1 and value2 however, {{value1}} and {{value2}} are not expanded and their values are not displayed.
Moving
Equals {{value1}}outside of the equals statement works as expected and {{value1}} is properly expanded
We experienced the same issue, and it can be solved by doing the following:
{{#equals value1 value2}}
Equals {{ @root.value1 }}
{{else}}
Not Equals {{ @root.value2 }}
{{/equals}}
Sorry for the delayed response on this. Thank you all for your patience. The coerced values should be working in the preview now, but I'll work through some of these examples to see where we can provide better examples in the docs.
I just ran into this issue myself and tried the suggestion of using {{ @root.value1 }} to no avail. @astuteape is there anything I could provide for further troubleshooting?
Hi, I can also confirm what @spaceribs , @ravihugo and @oliviercwa said. Variables inside {{equals}} are not rendered in preview mode, but are shown when I send a test E-Mail.
Would be great to have those variables available in preview mode :)
EDIT: Apparently it only works in Preview Mode and not in an actual email.
I found a solution for my scoping issues in this thread [https://stackoverflow.com/questions/13645084/access-a-variable-outside-the-scope-of-a-handlebars-js-each-loop]. My issue was that the {{#equals}}-block created its own scope which made variables outside inaccessible except for with the @root method as previously mentioned. If you prepend the the variable with ../myVariable you can navigate up one step in the hierarchy. It now works in the preview-mode for me.
{{#each @root.message.body}}
{{#equals this.component "textBlock_lite"}}
<p>{{../this.content}}</p>
{{/equals}}
{{/each}}
I found a solution for my scoping issues in this thread [https://stackoverflow.com/questions/13645084/access-a-variable-outside-the-scope-of-a-handlebars-js-each-loop]. My issue was that the {{#equals}}-block created its own scope which made variables outside inaccessible except for with the @root method as previously mentioned. If you prepend the the variable with ../myVariable you can navigate up one step in the hierarchy. It now works in the preview-mode for me.
{{#each @root.message.body}} {{#equals this.component "textBlock_lite"}} <p>{{../this.content}}</p> {{/equals}} {{/each}}EDIT: Apparently it only works in Preview Mode and not in an actual email.
@johannes9108 move your last line of EDIT to be the first line please