Search code examples
handlebars.jshandlebars.net

Handlebars If Statement not behaving as expected


I have the following json object -

{ 
    "type": "typeOne", 
    "Children": [
          {
             "ChildType": "ChildTypeOne",
             "Settings": {
                  "IsChildTypeOne": true
              }
           },
           {
               "ChildType": "ChildTypeTwo",
               "Settings": {
                    "IsChildTypeTwo": true
                }
           }
     ] 
}

My handlebars template contains the following snippet -

{{#each Children}}
    {{#if Settings.IsChildTypeOne}}
        ChildTypeOne!!
    {{else}}
        ChildTypeTwo!!
    {{/if}}
{{/each}}

If I run this data through the template, the only thing that ever renders is ChildTypeTwo!!. So it seems that the if statement isn't properly evaluating IsChildTypeOne. The strange part is that if I put a statement in to display the value of IsChildTypeOne in the else clause, the value is displayed as true for the first ChildType.

Does anyone have any thoughts as to why this is not working as expected?

NOTE - the json posted above is a trimmed down version of my actual object. The real object has nested Children arrays that reuse the same object structure. So for instance, in my example, ChildTypeOne can also have a Childrens array with other objects within it.

EDIT**** So in stepping through the code, I found that if I had my type defined as follows -

...
"Settings" : {
   "IsChildTypeOne": 'true'
}
...

it appears to work. Removing the single quoted causes the value to be read as undefined when stepping through.


Solution

  • This ended up being related to the process being used to serialize the json string into an object. Please see the issue here for an explanation.