Meteor.js Handlebars template logic operators

久未见 提交于 2019-12-11 02:37:50

问题


From this page, I inserted to my /client/helpers/handlebars.js file this handlebars helper:

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);
}

});

And to the template:

{{#compare "Test" "Test"}}
Default comparison of "==="
{{/compare}}

And in console I always see: Exception from Deps recompute: Error: Handlerbars Helper 'compare' needs 2 parameters

I tried this as well:

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

But this did not help.


回答1:


Try

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


来源:https://stackoverflow.com/questions/17252750/meteor-js-handlebars-template-logic-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!