Sendgrid templates: Variable substituition not working inside most conditionals
Variable substitution nested in greaterThan, lessThan, equals, notEquals, and and or conditionals is not displaying properly in the dynamic templates editor. The if and unless conditionals do display the expected value.
Note: The email that was sent has rendered correctly, indicating that this is a bug only within the editor.
Template:
<html>
<head>
<title></title>
</head>
<body>
<div>
<p>
{{#equals customerCode winningCode}}
You have a winning code: {{ customerCode }}
{{/equals}}
</p>
</div>
</body>
</html>
Test data:
{
"customerCode": 100,
"winningCode": 100
}
Expected content
You have a winning code: 100
Actual content
You have a winning code:
Link to page: https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements
I encountered this and found that using @root.fieldName seemed to work correctly in the editor... I have not yet tried that with an actual email though... Will update once I'm able to test using the root.
I encountered this and found that using
@root.fieldNameseemed to work correctly in the editor... I have not yet tried that with an actual email though... Will update once I'm able to test using the root.
It worked both on editor and on sending mails! Thank you!
Thank you for bringing this to our attention. We will investigate the issue and update with what we find.
Whats the status of this? We also have this and its really bad we cant use variables inside conditionals...
edit: Actually {{#each @root.items}} solves this case also. My bad.
~@cfogrady works for simple substitutions, but not for "each" loops inside equals. Anyone had success with this?~
{{#equals status "visible"}}
<div>
<strong>{{@root.value}} works</strong>
<ul>
{{#each items}}
<li>{{this}} does not work</li>
{{/each}}
</ul>
</div>
{{/equals}}
Just tested this: The editor's Preview page will not render the results of a comparison between coerced values. You can see the comparisons between coerced values only in a delivered message. (Try to send a test message)
I encounter this issue as well!! Below works in editor preview & actual received email
{{#greaterThan coupon_deduct 0}}
<div>${{@root.coupon_deduct}</div>
{{/greaterThan}}
I think sendgrid should update their documentation, instead of no follow up on the issue (people paid for your service!) I contact CS too, but they ask me to open issue in Github...
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}}
Same issue
{{#each data}}
{{#equals sum 1}}
{{../this.showData}} <- this works in preview doesn't work in email
{{/equals}}
{{/each}}
so what's the point to have a feature to work in preview if it doesn't work in real emails sent?
Same issue
{{#each data}} {{#equals sum 1}} {{../this.showData}} <- this works in preview doesn't work in email {{/equals}} {{/each}}so what's the point to have a feature to work in preview if it doesn't work in real emails sent?
can you post your JSON payload?
I have the same problem. The preview in the browser behaves differently than the mail rendering, which is not ideal.
This works in the preview:
{{#each items}}
...
{{#notEquals this.linePrice this.originalPrice}}
<span style="text-decoration: line-through">
{{@root.currency}} {{../this.originalPrice}}
</span>
{{/notEquals}}
...
{{/each}}
This works when sending the mail:
{{#each items}}
...
{{#notEquals this.linePrice this.originalPrice}}
<span style="text-decoration: line-through">
{{@root.currency}} {{this.originalPrice}}
</span>
{{/notEquals}}
...
{{/each}}
Data
{
"currency": "EUR",
"items": [
{"linePrice": "89.00", "originalPrice": "99.00"}
]
}
I couldn't find a way to make it work in both cases.
We had the similar problem where we tried to use this:
{{#greaterThan variable 0}}
<span>{{variable}}</span>
{{/greaterThan}}
It didn't show value of the variable in the mail or in the preview of the design library.
So what solved our problem was adding ../ inside the conditional block.
{{#greaterThan variable 0}}
<span>{{../variable}}</span>
{{/greaterThan}}
Maybe it will help someone who stumbles upon this issue. Cheers!
I have the same problem. The preview in the browser behaves differently than the mail rendering, which is not ideal.
Encountering the same issue provided here, the preview is different than the rendering. This is far from ideal for testing.
Taking this simple example with a json payload like:
{
data: [
{
sum: 1,
showData: "text to show"
}
]
}
this renders in the preview:
{{#each data}}
{{#equals sum 1}}
{{../this.showData}} <- this works in preview doesn't work in an actual email
{{/equals}}
{{/each}}
this renders in the email:
{{#each data}}
{{#equals sum 1}}
{{this.showData}} <- this works only in an actual email, breaks in the preview
{{/equals}}
{{/each}}