从this page,我插入了我的/client/helpers/handlebars.js文件这个把手助手:
Handlebars.registerHelper('compare', function (lvalue, operator, rvalue, options) {
var operators, result;
if (arguments.length < 3) {
throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
}
if (options === undefined) {
options = rvalue;
rvalue = operator;
operator = "===";
}
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; },
'>=': function (l, r) { return l >= r; },
'typeof': function (l, r) { return typeof l == r; }
};
if (!operators[operator]) {
throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
}
result = operators[operator](lvalue, rvalue);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
到模板:
{{#compare "Test" "Test"}}
Default comparison of "==="
{{/compare}}
在控制台中我总是看到:Deps重新计算的例外:错误:Handlerbars帮助'比较'需要2个参数
我也尝试了这个:
{{#compare "Test" "==" "Test"}}
但这没有帮助。
答案 0 :(得分:0)
尝试
{{#compare "Test" "Test" operator="=="}}