Search code examples
templatesmeteoriron-router

Meteor each loop within template


So I'm looking for some understanding on how templates and such work.

I've movie.html file with a template named movies in it, with a movie.js helper that returns a collection.

Movies Template

<template name="movies">
   <li>{{title}}</li>
</template>

JS Helpers

Template.body.helpers({
  movie: function () {
    return Movies.find({});
    }
  });

Now I've another template that does a bunch of other things but one of them is to iterate over this list and display it.

List Template

<template name="list">
    <ul>
        {{#each movie}}
            {{> movies}}
        {{/each}}
    </ul>
</template>

In this situation the list doesn't popular with the data.

However, if I move the contents of the list template outside of a template and just on the main.html it works great!

This is how I used to use it but I've started to use Houston Admin Package which uses Iron:Router so I've moved the main (and only) page to a template for routing purposes, which breaks my looping list.

I'm sure I'm missing something minor but I can't figure it out.


Solution

  • You are using the {{#each movie}} helper on the list Template so change the Template.helper to the list template

    Template.list.helpers({
      movie: function () {
        return Movies.find({});
        }
      });
    

    We are you calling this <template name="list"> on the body tag? you have something like this.

    <body>
     {{> list}}
    </body>
    

    Or you have something like this.

    <template name="layout">
     {{> yield}} <!-- since you are mentioning iron:route package -->
    </template>
    

    Or you have a route to that list template? localhost:3000/lists

    Router.route('/movie-lists', function () {
      this.render('lists')
    });
    

    On whatever of this 3 cases, you should point the helper into the template where you are calling it (in this case list template)