Sharing functions between templates in Meteor

meteor

Solution

According to the Meteor documentation, the correct way would be to use UI namespace, which would bind to whatever template engine used underneath, instead of using Handlebars, or spacebars directly. Here is the link in documentation. http://docs.meteor.com/#ui_registerhelper

So for generic function to be accessed from any template use in client.js:

UI.registerHelper('stub', function() {
     // code
});

Update:

Apparently the docs have changed back to using:

Template.registerHelper('stub', function() {
     // code
});

Then in the the html file use `{{stub}}` to access the variable.

Problem

What if I have two handlebar templates that want to check the return value of the same function? Is there a right way to do this? Obviously I could do this: ``` var say_foo = function() { alert('foo'); }; Template.foo.say_foo = say_foo; Template.bar.say_foo = say_foo; ``` But is there some way to do this directly through the Template object? Since this is JavaScript I can assign a function to any property of any object, but I tried doing: ``` Template.say_foo = function() { alert('foo'); }; ``` and of course the templates don't find this function. Just curious about best practices and the like. Thanks!

Original source