Handlebars.js {{#if}}
helper lacks of equality condition. In cases where some logic should be put inside the template, some folks prefer to override it. But instead of overriding {{#if}}
helper, and taking my chances to break previous/older handlebars templates in my projects, I’m always about to use my custom helper {{#ifequal}}
.
1 2 3 4 5 6 7 8 |
Handlebars.registerHelper('ifequal',function(conditional,options) { if (options.hash.value === conditional) { return options.fn(this); } else { return options.inverse(this); } }); |
Usage
1 2 3 4 5 6 7 |
type = "password"; {{#ifequal type value="password"}} <input type="password"> {{else}} <input type="text"> {{/ifequal}} |