Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two ways to define helpers in Meteor

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.

like image 371
Andrew Mao Avatar asked Dec 08 '25 23:12

Andrew Mao


1 Answers

According to http://docs.meteor.com/#template_helpers they are equivalent aside from the syntax, and the Template.myTemplate.foo syntax will not work for reserved template names.

The nice thing about using a dictionary passed to Template.myTemplate.helpers is that you can reuse it across multiple templates.

    var reusableHelpers = { stuff: function() { return "stuff"; } };
    Template.foo.helpers( reusableHelpers );
    Template.bar.helpers( reusableHelpers );
like image 176
Ryan Yeske Avatar answered Dec 10 '25 22:12

Ryan Yeske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!