Handlebars組み込みHelperのIFにはオブジェクトの有無の判別しかない。なので、値の一致や大小比較ができないので、Helperを作って対応する。
コード
Handlebars.registerHelper('cond', function (v1,cond,v2, options) { if((cond === '==' || cond === 'eq') && v1 === v2) { return options.fn(this); } if((cond === '!=' || cond === 'ne') && v1 !== v2) { return options.fn(this); } if((cond === '>=' || cond === 'ge') && v1 >= v2) { return options.fn(this); } if((cond === '<=' || cond === 'le') && v1 <= v2) { return options.fn(this); } if((cond === '>' || cond === 'gt') && v1 > v2) { return options.fn(this); } if((cond === '<' || cond === 'lt') && v1 < v2) { return options.fn(this); } return options.inverse(this); });
簡易テスト
{{#cond 'hoge' '==' 'hoge' }} true {{else}} false {{/cond}} {{#cond 'hoge' '==' 'fuga' }} true {{else}} false {{/cond}} {{#cond 3 '==' 4 }} true {{else}} false {{/cond}} {{#cond 3 '!=' 4 }} true {{else}} false {{/cond}} {{#cond 3 '>=' 4 }} true {{else}} false {{/cond}} {{#cond 3 '<=' 4 }} true {{else}} false {{/cond}} {{#cond 3 '>' 4 }} true {{else}} false {{/cond}} {{#cond 3 '<' 4 }} true {{else}} false {{/cond}} {{#cond 3 'eq' 4 }} true {{else}} false {{/cond}} {{#cond 3 'ne' 4 }} true {{else}} false {{/cond}} {{#cond 3 'ge' 4 }} true {{else}} false {{/cond}} {{#cond 3 'le' 4 }} true {{else}} false {{/cond}} {{#cond 3 'gt' 4 }} true {{else}} false {{/cond}} {{#cond 3 'lt' 4 }} true {{else}} false {{/cond}}
結果
true false false true false true false true false true false true false true
複数条件への対応など複雑な事はできないが、多少は役に立つだろう。