简体   繁体   English

handlebars.js {{#ifEquals}}中的多个OR运算符是有条件的

[英]Multiple OR operators in a handlebars.js {{#ifEquals}} conditional

I'm trying to do the following but I get an error: 我正在尝试执行以下操作但是出现错误:

{{#ifEquals nominatorRegion "BC Region" || nominatorRegion "Saskatchewan Region" || nominatorRegion "Alberta Region"}}

Error: 错误:

Uncaught Error: Parse error on line 60:
...rRegion "BC Region" || nominatorRegion "
-----------------------^
Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'CLOSE_BLOCK_PARAMS'
    at a.parseError (handlebars.min.js:28)
    at a.parse (handlebars.min.js:28)
    at d [as parse] (handlebars.min.js:27)
    at d (handlebars.min.js:28)
    at child.e [as template] (handlebars.min.js:28)
    at child.render (blog.js:289)
    at Object.BlogApp.fn.renderView (blog.js:1266)
    at success (blog.js:987)
    at parse-1.2.19.js:3858
    at wrappedResolvedCallback (parse-1.2.19.js:3762)

My Handlebars comparison scripts: 我的Handlebars比较脚本:

<script>
    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);
            default:
            return options.inverse(this);
        }
    });
</script>

<script>
    Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
        return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
    });
</script>

First your helper ifEquals accepts only 2 arguments and your are passing much more than two arguments. 首先你的助手ifEquals只接受2个参数,你传递的参数多于两个。 Actually the || 实际上是|| operator that you use is one argument and not an operator as you can think. 您使用的运算符是一个参数而不是您可以想到的运算符。 Try chaining your tests if you need &&/and operator and if you need a ||/or operator try duplicating the blocks. 如果需要&& /和运算符,请尝试链接测试,如果需要|| /或运算符,请尝试复制块。

Second if you want to pass arguments you have to use '||' 第二,如果你想传递参数,你必须使用'||' or '&&' otherwise it would lookup your data and || 或'&&'否则它将查找您的数据和|| is not a field of your data. 不是您的数据字段。

Third another method would be to write a Helper that would accept much more arguments you'll find how to do this in the snippet below : 第三种方法是编写一个Helper,它将接受更多参数,您将在下面的代码片段中找到如何执行此操作:

 $(document).ready(function () { var context = { "regions" : [{"nominatorRegion":"BC Region"},{"nominatorRegion":"Saskatchewan Region"},{"nominatorRegion":"Alberta Region"},{"nominatorRegion":"Delaware Region"},{"nominatorRegion":"Michigan Region"}] }; Handlebars.registerHelper('ifEqualsChained', function() { var options = arguments[arguments.length-1]; // Assuming that all wanted operator are '||' valueToTest=arguments[0]; for (var i = 1; i < (arguments.length - 1); i++) { if (valueToTest === arguments[i]) { return options.fn(this); } } return options.inverse(this); }); var source = $("#sourceTemplate").html(); var template = Handlebars.compile(source); var html = template(context); $("#resultPlaceholder").html(html); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script id="sourceTemplate" type="text/x-handlebars-template"> <ul> {{#each regions}} {{#ifEqualsChained nominatorRegion "BC Region" "Saskatchewan Region" "Alberta Region"}} <li> {{nominatorRegion}} </li> {{/ifEqualsChained}} {{/each}} </ul> </script> <div id="resultPlaceholder"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM