Jrom
Jrom

Reputation: 851

Is it possible to access multiple arrays in an #each loop in a handlebars file?

In app.js, I pass back 2 arrays of the same size to the client.

Is it possible to do something like this?

{{#each arrayOne}}
<Li>this </Li>
<Li> {{@index : arrayTwo}} </Li>
{{/each}}

Upvotes: 0

Views: 283

Answers (1)

bevacqua
bevacqua

Reputation: 48476

You should avoid complex logic in your view templates. Instead, I'd recommend having a controller do the merging job for you.

That way you'd untangle the view, since its more descriptive of what you're going to display, and your controller clearly states the intent to render different data in a combined manner.

e.g

model.arr = arrayOne.map(function (item, i) {
  return {
    i1: arrayOne[i],
    i2: arrayTwo[i]
  };
});

Then your view becomes much simplified

{{#each arr}}
<Li>{{arr.i1.thing}}</Li>
<Li>{{arr.i2.thingie}}</Li>
{{/each}}

Obviously you should improve this further by only returning the relevant properties in your map callback.

Upvotes: 1

Related Questions