javascriptmeteormeteor-blazespacebarsmeteor-helper

How can I put global template helpers together in Meteor?


I have several global template helpers

Template.registerHelper("termSuffix",function(){});

Template.registerHelper("subjects",function(){});

Template.registerHelper("date",function(){});
...

I tried this (like normal/local template helpers)

Template.registerHelper({
      termSuffix:function(){},
      subjects:function(){},
      date:function(){}
});

But it throws Exception from Tracker recompute function:Error: No such function: termSuffix


Solution

  • This syntax is not supported for global helpers. Docs Code

    If you think it's a compelling enough change to the library, you could submit a pull request.

    Meanwhile, you can wrap the Template.registerHelper function with your own:

    function registerGlobalHelpers(helpers){
        _.chain(helpers)
         .each( (fn, name) => { Template.registerHelper(name, fn); })
         .value();
      }
    

    or defined without chaining in underscorejs:

    function registerGlobalHelpers(helpers){
      _.each(helpers, (fn, name) => { Template.registerHelper(name, fn); });
      }
    

    Then use it like this:

    registerGlobalHelpers({
          termSuffix:function(){},
          subjects:function(){},
          date:function(){}
    })