I am trying to write a conditional if statement helper for Handlebars.js. Essentially, I want to put an \"active\" class on a link if it is the Apply Now
page.
NOTE: block(this) in the helper will not work anymore. Instead, use block.fn(this)
e.g.
Handlebars.registerHelper('isApplyNow', function(block) {
if (this.title === "Apply Now")
return block.fn(this);
else
return block.inverse(this);
});
I do see one minor syntax mistake which I believe could be the issue. If you are going to use a helper that takes a block, then you have to close it with the helper name. See how I've replaced your {{/if}}
with {{/isApplyNow}}
, like so:
{{#isApplyNow}}
<a href="{{url}}" class ='active'>{{this.title}}</a>
{{else}}
<a href="{{url}}">{{this.title}}</a>
{{/isApplyNow}}