-1

My array is:

[    {    status: failed    },    {    status: passed    }    ]

The if...else condition of my Handlebars.js template is:

<td><a class="{{#each status}} {{#if failed}} fail {{else}} pass {{/if}} {{/each}}"></a></td>

The above condition is not working. Any help will be appreciated.

0

1 Answer 1

0

There's a lot going on here. First off, failed and passed in your array are variables. Are they booleans, strings, or otherwise? Either way, the values of those variables are getting passed in to your template, not their name, so {{#if failed}} isn't going to work because it's looking for a variable with a name of 'failed' which doesn't exist. Also, each object in your array has a key of status, but {{#each status}} iterates over an array with a name of status, so there's a mismatch there as well.

Finally, I think your logic might be wrong- if this worked, it would have this output:

<td><a class="fail pass"></a></td>

because your loop is within a single <a> element. I'm guessing you want to have an individual table cell per status. If that's correct, you'd want something like this:

{{#each status}}<td><a class="{{#if failed}} fail {{else}} pass {{/if}}"></a></td>{{/each}}

with a data structure like:

{status: [{failed: true}, {failed: false}]}

like in this jsfiddle: http://jsfiddle.net/carpeliam/5fgr10gf/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.