最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Multiple OR operators in a handlebars.js {{#ifEquals}} conditional - Stack Overflow

matteradmin5PV0评论

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 parison scripts:

<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>

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 parison scripts:

<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>
Share Improve this question edited Feb 25, 2018 at 2:44 Martin Erlic asked Feb 25, 2018 at 2:25 Martin ErlicMartin Erlic 5,67726 gold badges92 silver badges162 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First your helper ifEquals accepts only 2 arguments and your are passing much more than two arguments. 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 :

$(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.pile(source);
  var html = template(context);
  $("#resultPlaceholder").html(html);

});
<script src="https://cdnjs.cloudflare./ajax/libs/handlebars.js/4.0.5/handlebars.js"></script>
<script src="https://ajax.googleapis./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>

Post a comment

comment list (0)

  1. No comments so far