javascript Meteor:在 Blaze 中测试两个值(如 {{#if someVar == 'someVal'}})相等性的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22705532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:37:22  来源:igfitidea点击:

Meteor: Whats the best way to test equality of two values (like {{#if someVar == 'someVal'}}) in Blaze?

javascriptmeteortemplating

提问by Patrick Canfield

I'm having to define template helpers everywhere that simply test the equality of a document property with a constant so I can do something like this in my template:

我必须在任何地方定义模板助手,只需用常量测试文档属性的相等性,这样我就可以在我的模板中执行以下操作:

    {{#if fruitIsPineapple}}...{{/if}}

And in my template helper looks like:

在我的模板助手中看起来像:

    Template.example.helpers({
      fruitIsPineapple: function () { return this.document.fruit === 'pineapple'; } 
    });

How can I save myself from having to create all these helpers? It'd be nice if there we an equality operator in Blaze...

我怎样才能避免自己创建所有这些助手?如果我们在 Blaze 中有一个相等运算符就好了......

回答by Patrick Canfield

I had my question answered at the Meteor Devshop. Turns out you can define a Handlebars helper, like so:

我的问题在 Meteor Devshop 得到了解答。原来您可以定义一个 Handlebars 助手,如下所示:

    Template.registerHelper('equals', function (a, b) {
      return a === b;
    });

Then use it in prefix position like this:

然后在前缀位置使用它,如下所示:

    {{#if equals fruit 'pineapple'}}...{{/if}}

回答by jasenkoh

Without any cumbersome code, you can achieve this by installing raix:handlebar-helpersand do something like this:

没有任何繁琐的代码,您可以通过安装raix:handlebar-helpers并执行以下操作来实现此目的:

{{#if $eq a b}}
   ...
{{ /if }}