Search code examples
handlebars.jssendgrid

SendGrid Handle bars variable in json not show up when used a second time


I have this handle bars statement in SendGrid. When using the variable taskCount the value is only used in the greaterThan block. The other 2 times it is used it appears to be null.

Here is the json data

{
    "Username":"ChampCbg",
    "JoinedAt":"12/1/2020",
    "DaysSinceJoined":"20",
    "taskCount":5
}

here is statement with handlebars

{{#greaterThan taskCount 0}} 
Congrats on starting {{insert taskCount "default=1"}} task{{#greaterThan taskCount 1}} (s){{/greaterThan}} and taking the first small step. 
{{else}}
You have not started a task yet. What are you waiting for? It has been {{DaysSinceJoined}} days since you joined on {{JoinedAt}}. 

You have missed {{DaysSinceJoined}} days where you could been making Small Steps towards the dreams of you better tomorrow. 
{{/greaterThan}}

Here is the end result

Hello ChampCbg!

Congrats on starting 1 task and taking the first small step.

Don't let another day pass you by. Start designing you vision board today.

As the results show the greaterThan block selects the correct statement, but then next times taskCount is used the default value and null value are chosen.

What is the cause of this?


Solution

  • Twilio SendGrid developer evangelist here.

    Honestly, I thought your template would work as you wrote it. But I tried it out and it did not (not that I didn't believe you, I just had to do that to work out what to do!).

    So, the way to deal with this is to refer to the variables in your data using the @root object within the greaterThan conditional (or other conditionals).

    Try this as your template:

    {{#greaterThan taskCount 0}} 
    Congrats on starting {{insert @root.taskCount "default=1"}} task{{#greaterThan @root.taskCount 1}} (s){{/greaterThan}} and taking the first small step. 
    {{else}}
    You have not started a task yet. What are you waiting for? It has been {{@root.DaysSinceJoined}} days since you joined on {{@root.JoinedAt}}. 
    
    You have missed {{@root.DaysSinceJoined}} days where you could been making Small Steps towards the dreams of you better tomorrow. 
    {{/greaterThan}}