admin管理员组文章数量:1353311
I need a way to pare values in handlebars and found this helper on the web:
Handlebars.registerHelper('pare', function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlerbars Helper 'pare' needs 2 parameters");
var operator = options.hash.operator || "==";
var operators = {
'==': function(l,r) { return l == r; },
'===': function(l,r) { return l === r; },
'!=': function(l,r) { return l != r; },
'<': function(l,r) { return l < r; },
'>': function(l,r) { return l > r; },
'<=': function(l,r) { return l <= r; },
'>=': function(l,r) { return l >= r; },
'typeof': function(l,r) { return typeof l == r; }
}
if (!operators[operator])
throw new Error("Handlerbars Helper 'pare' doesn't know the operator "+operator);
var result = operators[operator](lvalue,rvalue);
if( result ) {
return options.fn(this);
} else {
return options.inverse(this);
}
This is how I use it:
{{#each numbers as |nr|}}
{{#pare nr stars operator="<=" }}
<span{{action "rate" }} class="rating{{nr}} glyphicon glyphicon-star"></span>
{{/pare}}
{{/each}}
});
I get this error:
TypeError: options.inverse is not a function
I need a way to pare values in handlebars and found this helper on the web:
Handlebars.registerHelper('pare', function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlerbars Helper 'pare' needs 2 parameters");
var operator = options.hash.operator || "==";
var operators = {
'==': function(l,r) { return l == r; },
'===': function(l,r) { return l === r; },
'!=': function(l,r) { return l != r; },
'<': function(l,r) { return l < r; },
'>': function(l,r) { return l > r; },
'<=': function(l,r) { return l <= r; },
'>=': function(l,r) { return l >= r; },
'typeof': function(l,r) { return typeof l == r; }
}
if (!operators[operator])
throw new Error("Handlerbars Helper 'pare' doesn't know the operator "+operator);
var result = operators[operator](lvalue,rvalue);
if( result ) {
return options.fn(this);
} else {
return options.inverse(this);
}
This is how I use it:
{{#each numbers as |nr|}}
{{#pare nr stars operator="<=" }}
<span{{action "rate" }} class="rating{{nr}} glyphicon glyphicon-star"></span>
{{/pare}}
{{/each}}
});
I get this error:
Share Improve this question asked Oct 8, 2015 at 6:25 LukeLuke 1,8142 gold badges22 silver badges32 bronze badgesTypeError: options.inverse is not a function
2 Answers
Reset to default 4You need to define the else
section for pare block helper.
Handlebars provides the block for the else fragment as options.inverse. You do not need to check for the existence of the else fragment: Handlebars will detect it automatically.
Source: https://handlebarsjs./guide/block-helpers.html#conditionals
Another option is to go with principle of least surprise in the helper:
if (typeof options.inverse === "function")
return options.inverse(this);
else
return null;
Which isn't bad practice for any Handlebars helper; don't try to render an inverse if the template writer didn't provide one.
本文标签: javascriptTypeError inverse is not a function (in handlebars helper)Stack Overflow
版权声明:本文标题:javascript - TypeError: inverse is not a function (in handlebars helper) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743927128a2563166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论