在handlebars.js {{#if}}条件下的逻辑运算符解决方案
2023-09-11 14:21:35 时间
解决方案。这增加了比较运算符。
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 operato
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
相关文章
- js将/Date(1642563573347)/转成可读的日期格式,js时间戳转换可读的日期格式
- JQ,js给所有table元素标签添加样式类,并且外面再套一个div
- js_JavaScript中有六种值为“假”
- 【微信小程序】--WXML & WXSS & JS 逻辑交互介绍(四)
- js 秒转换成 时分秒
- 如何在2016年成为一个更好的 Node.js 开发者
- js实例分析JavaScript中的事件委托和事件绑定
- 《Node.js入门经典》一1.3 安装并创建第一个Node.js程序
- JS 数组方法 every 和 some 的区别
- JS输出内容为[object Object]与toString
- iframe标签解析腾讯视频链接的获取不到总时长的解决方案(Txp.play.js)
- JS: javascript 点击事件执行两次js问题 ,解决jquery绑定click事件出现点击一次执行两次问题
- JS 将数字字符串数组转为 数字数组 (互换),js获取数组对象中 某一个key的值,js判断一个数组是否包含另一个数组(一维数组)
- JS 时间转换函数 字符串时间转换毫秒(互转)
- WEB前端大作业-个人资料展示响应式网页模板(HTML JS CSS)
- [js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)
- (cocos2d-js游戏)測试你的反应速度----------基本逻辑(上)
- JQuery/JS插件 linq.js 入门
- Three.js Example 注解 —— canvas_materials_video.html