Search code examples
meteoriron-router

Passing data from iron router into template helper


I am trying to pass in the categoryId from the iron router data into the template helper in meteor.

This is my router code:

Router.route('/lessons/:categoryId', function() {
this.subscribe('lessons');
this.render('Lessons', {
    data: {
        categoryId: this.params.categoryId
    }
});

This is my template code:

Template.Lessons.helpers({
lessons: function () {
console.log('CategoryId: '+categoryId);
}
});

How can I correctly access the categoryId that was created in iron router?

Thanks so much for any help.


Solution

  • data from your router provides the context (this) for your template. To access categoryId from your helper, use this.categoryId:

    Template.Lessons.helpers({
      lessons: function() {
        console.log('CategoryId: ' + this.categoryId);
      }
    });
    

    You can also access router data via:

    Template.instance().data.categoryId;