Access Template.TemplateName.helpers from Template.helpers and vice versa

135 Views Asked by At

Is there a way to gain access to the Template instance from a global helper and vice versa?

/lib/route.js (with Iron Router):

Router.route('/', {name: 'home.view', controller: 'homeController'});
homeController = RouteController.extend({
    template: 'home',
    waitOn: function () {
      Meteor.subscribe("Person", Meteor.userId());
    },
    data: function () {
        // return some data;
    }
});

homeController.helpers({
   templateInstanceHelper: function () {
      // Access a "global" helper here
   }
});

/client/helpers.js:

Template.helpers("globalHelper", function () {
   // Access the template instance helper here
});
1

There are 1 best solutions below

1
On

Have you considered defining a global method instead? Instead of registering with Meteor Templates, just define it as

globalHelperFunc = function(templateVar) {
   // do work
}

Note that this need to be in "lib" folder, so maybe (/lib/helpers.js)

Reference: Global function for Meteor template helper