admin管理员组文章数量:1024629
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
1 Answer
Reset to default 4First 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>
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
1 Answer
Reset to default 4First 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>
本文标签: javascriptMultiple OR operators in a handlebarsjs ifEquals conditionalStack Overflow
版权声明:本文标题:javascript - Multiple OR operators in a handlebars.js {{#ifEquals}} conditional - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610222a2158959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论