2

这个页面,我将这个把手助手插入到我的 /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 重新计算异常:错误:处理程序栏助手“比较”需要 2 个参数

我也试过这个:

{{#compare "Test" "==" "Test"}}

但这并没有帮助。

4

1 回答 1

0

尝试

{{#compare "Test" "Test" operator="=="}}
于 2013-06-22T17:04:24.140 回答