Meteor.js Handlebars模板逻辑运算符 [英] Meteor.js Handlebars template logic operators

查看:148
本文介绍了Meteor.js Handlebars模板逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此页面,我插入到我的/client/helpers/handlebars.js文件中的这个句柄帮助器:

  Handlebars.registerHelper('compare',function (lvalue,operator,rvalue,options){

var操作符,结果;

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 1> = r; },
'typeof':function(l,r){return typeof l == r; }
};

if(!operators [operator]){
throw new Error(Handlerbars Helper'compare'不知道操作符+操作符);
}

result = operators [operator](lvalue,rvalue);

if(result){
return options.fn(this);
} else {
return options.inverse(this);
}

});

以及模板:

<$ p $ {{#compareTestTest}}
===的默认比较
{{/比较}}
$ b

我也试过这个:

  {{#compareTest==测试}} 

但这没有帮助。

解决方案

试试

  {{#compareTest ===}} 


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.

解决方案

Try

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

这篇关于Meteor.js Handlebars模板逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆