5

While this is rendered as expected:

{{#each Items}}  // a collection
  {{title}}  
{{/each}}  
{{#each types}}  // another ollection
  {{refId}}  
{{/each}}  

if I put it together:

{{#each Items}}  
  {{title}}  
  {{#each types}}  
    {{refId}}  
  {{/each}} 
{{/each}} 

The #each types is empty.

The template helpers:

Template.menuItems.helpers({
    restMenuItems: function() {
        return RestMenuItems.find({restRefId: this._id}, {sort:Template.instance().sort.get()});
    },
    restMenuSideItems: function() {
        return RestMenuSideItems.find({restRefId: this._id},            {sort:Template.instance().sort.get()});
    }
});

Template.menuItems.onCreated( function(){
    this.subscribe('restMenuItems');
    this.subscribe('restMenuSideItems');
});

And some of the template code:

{{#each restMenuItems}}
  <div>
    <select class="list-group select_side_addons">
      {{#each restMenuSideItems}}
        <option>{{restRefId}}</option>
      {{/each}}
    </select>
  </div>
{{/each}}

Even when replacing {{#each restMenuSideItems}} by {{#each ../restMenuSideItems}}, nothing appears.

What's wrong?

1 Answer 1

3

Because the #each clause changes the data context to the current item.

If you want a list of types inside each of the Items, you can get the parent data context using ...

If types is an attribute of the parent context:

{{#each Items}}  
  {{title}}  
  {{#each ../types}}  
    {{refId}}  
  {{/each}} 
{{/each}} 

If types is a template helper, you can pass the parent context as an argument to it:

{{#each Items}}  
  {{title}}  
  {{#each types ..}}  
    {{refId}}  
  {{/each}} 
{{/each}} 

and use the context for your query.

Template.myTemplate.helpers({

  types: function(parent) {
    // you now have the parent context here.
    // use parent._id etc. where you used this._id in the
    // "flat" version.
  }
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but it is still the same.
Strange, that should work. Can you provide more code (e.g, your helpers and more complete template code)? Where do Items and types come from?
Just did. @MasterAM
With a different collection inside the each it works without the need to use the ../, I guess because they don't have fields with the same keys.
The issue is likely not with it being the same/different collection, but with the fact that it is a helper using the data context for the query. Passing the parent data context as a parameter should do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.