Search code examples
javascriptmeteoriron-router

Pass variable to template callback


Recently I started getting familiar with meteor platform, and I stumbled on dilemma: Is there a way to pass variables to Template.rendered, Template.created callbacks. Let's say I have route

Router.route('/profile/:_id', {
    name: 'profile'
});

and I want to somehow to pass that _id variable to Template.rendered callback:

Template.profile.rendered = function () {
    //how can I get "_id" in here?
};

Is this possible? If so how can I do it?


Solution

  • Usually when you declare a route, you also specify a data context to provide to the template that will be rendered :

    Router.route('/profile/:_id', {
      name: 'profile',
      data: function(){
        return Meteor.users.findOne(this.params._id);
      }
    });
    

    This way you can reference the user _id coming from the current data context assigned to the profile template like this :

    Template.profile.rendered = function () {
      console.log(this.data._id);
    };