docs icon indicating copy to clipboard operation
docs copied to clipboard

Sendgrid templates: Variable substituition not working inside most conditionals

Open fskuteken opened this issue 6 years ago • 13 comments

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

fskuteken avatar Sep 14 '20 03:09 fskuteken

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.

cfogrady avatar Sep 18 '20 19:09 cfogrady

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.

It worked both on editor and on sending mails! Thank you!

guilhermecampello avatar Sep 18 '20 20:09 guilhermecampello

Thank you for bringing this to our attention. We will investigate the issue and update with what we find.

wadec9n avatar Sep 18 '20 22:09 wadec9n

Whats the status of this? We also have this and its really bad we cant use variables inside conditionals...

Preen avatar Jan 07 '21 15:01 Preen

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}}

petrusgomes avatar Feb 14 '21 15:02 petrusgomes

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)

rionCodaTech avatar Mar 15 '21 20:03 rionCodaTech

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...

kc980602 avatar Mar 16 '21 16:03 kc980602

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}}

johannes9108 avatar Mar 17 '21 13:03 johannes9108

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?

Shawnxkwang avatar May 05 '21 17:05 Shawnxkwang

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?

rionCodaTech avatar May 05 '21 20:05 rionCodaTech

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.

pkraeutli avatar Jun 02 '21 10:06 pkraeutli

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!

Milutin-P avatar Jun 02 '21 13:06 Milutin-P

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}}

konradjniemiec avatar Jun 17 '21 16:06 konradjniemiec