• 在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
  • 相关阅读:
    【IT学习资源】2013.10.30
    【转载】 Bash之read命令
    【书本目录】 -- ABS(advanced bash scripts)
    【转载】vSphere的使用
    【转载】 Linux命令 -- tr 转换字符
    【转载】Oracle的日常监控脚本
    【转载】Nginx基础:6.webcache缓存服务
    大学记忆(3)[三国杀(终)]
    大学记忆(1)[记忆之殇]
    大学记忆(2)[计算机]
  • 原文地址:https://www.cnblogs.com/gopark/p/8762441.html
Copyright © 2020-2023  润新知