RMontes13
RMontes13

Reputation: 2148

Handlebars with Backbone iterating trouble

I have a problem with a "each" loop in a handlebars template:

I pass to the handlebars template one object as follows:

var data = {
   blog = blogModel, // Backbone Model
   user = userModel  // Backbone Model
}

this.el.html(template(data.toJSON()))

This is my models structure:

blogModel

  title: "myblog", 
  posts: [{
        text: "first post",
        datetime: "12/10/2010
      },
      {
        text: "second post",
        datetime: "10/10/2010
      }
        ...
  ]

userModel

  name: "John",
  email: "[email protected]"

Handlebars template

{{#each blog.posts}}
   <div>{{title}}</div>
    ...
   <span>{{user.email}}</span>
{{/each}}

My problem is that I can´t output {{user.email}} because it´s in the context of {{#each blog.posts}}, it seems that only can output the blog properties.

If I put the {{user.email}} out of the loop it works

Upvotes: 0

Views: 69

Answers (1)

mu is too short
mu is too short

Reputation: 434585

You can step up one level in the scope using ../ so something like this should work:

{{#each blog.posts}}
    ...
   <span>{{../user.email}}</span>
{{/each}}

Upvotes: 4

Related Questions