Search code examples
meteoriron-router

iterate over data from a route in a template


I have a cursor returned from a route, how can I use the data as context in a template?

example:

router.js

this will return a cursor with all documents where parent is equal to the params_id

this.route('my route', {
    path: '/myroute:_id',
    data: function(){
        return MyCollection.find({parent: this.params._id});
    }
});

how should my template look like to "iterate" over the cursor? normally if I use MyCollection.find({}) I iterate with #each and give the context a name via a TemplateHelper. I guess

{{#each data}}....{{/each}}

should be right but it doesn´t work.


Solution

  • Setting data in the route sets the context for the template. In the template, the context is accessed via this:

    {{#each this}}...{{/each}}
    

    Alternatively, if you prefer to assign a name to your data, can return an object from the route:

    data: function(){
      return {posts: Posts.find({parent: this.params._id})};
    }
    

    And then you can iterate over the documents like so:

    {{#each posts}}...{{/each}}