handlebars.js{#if}条件中的逻辑运算符JS中是否有一种将逻辑运算符合并到标准handlebars.js条件运算符中的方法?就像这样:{{#if section1 || section2}}.. content{{/if}}我知道我可以写我自己的助手,但首先我想确保我没有重新发明车轮。
3 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '!=':
return (v1 != v2) ? options.fn(this) : options.inverse(this);
case '!==':
return (v1 !== v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}});{{#ifCond var1 '==' var2}}Handlebars.registerHelper 'ifCond', (v1, operator, v2, options) -> switch operator when '==', '===', 'is' return if v1 is v2 then options.fn this else options.inverse this when '!=', '!==' return if v1 != v2 then options.fn this else options.inverse this when '<' return if v1 < v2 then options.fn this else options.inverse this when '<=' return if v1 <= v2 then options.fn this else options.inverse this when '>' return if v1 > v2 then options.fn this else options.inverse this when '>=' return if v1 >= v2 then options.fn this else options.inverse this when '&&', 'and' return if v1 and v2 then options.fn this else options.inverse this when '||', 'or' return if v1 or v2 then options.fn this else options.inverse this else return options.inverse this
添加回答
举报
0/150
提交
取消
