zoukankan      html  css  js  c++  java
  • 在handlebars.js {{#if}}条件下的逻辑运算符解决方案

    解决方案。这增加了比较运算符。

    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
  • 相关阅读:
    GCD实现多个定时器,完美避过NSTimer的三大缺陷(RunLoop、Thread、Leaks)
    iOS适配UIViewView/WKWebView,H5生成长图,仿微信进度条
    翻译jquery官方的插件制作方法
    javascript引用和赋值
    薯片公司真实JS面试题(乐视TV)
    caller、call、apply、callee的用法和意思
    常用javascript类型判断
    Git 常用命令笔记(不定期持续记录)
    sublime text2 emmet 安装
    hash"#"
  • 原文地址:https://www.cnblogs.com/gopark/p/8762441.html
Copyright © 2011-2022 走看看