2

EDIT: This is no longer relevant in Meteor 1.0. The first syntax has been deprecated, and only the second is supported.

It seems that there are two ways to define what are apparently called helpers in Meteor:

Template.foo.helper1 = function() { ... }

Other way:

Template.foo.helpers({
  helper2: function() { ... }
});

Is there any semantic or usage difference between the two? The only restriction I can see is that the first usage can't use reserved keywords. I'm wondering if the distinction arose only for historical reasons or if there is something else.

4

3 回答 3

9

根据http://docs.meteor.com/#template_helpers除了语法之外它们是等价的,并且该Template.myTemplate.foo语法不适用于保留的模板名称。

使用传递给的字典的好处Template.myTemplate.helpers是您可以在多个模板中重用它。

    var reusableHelpers = { stuff: function() { return "stuff"; } };
    Template.foo.helpers( reusableHelpers );
    Template.bar.helpers( reusableHelpers );
于 2013-06-14T02:06:33.090 回答
0

正如那里回答的那样:在 Meteor 中的模板之间共享功能,现在有了 Meteor 1.0,这似乎已经改变了。经过一番挖掘,我发现您现在可以使用以下内容在助手之间重用代码:

https://docs.meteor.com/#/full/template_registerhelper

Template.registerHelper("checkedIf",function(value){
  return value?"checked":"";
});

这使它在所有助手中都可用!

于 2014-11-03T11:38:35.993 回答
0

我认为您正在寻找的是有关车把的详细信息,可在此处找到:

http://handlebarsjs.com/expressions.html

于 2013-06-14T01:53:32.220 回答