2

I have the following arrays in my JavaScript:

let food = [
    'taco',
    'burrito',
    'hamburger',
  ]

let drinks = [
    'water',
    'milk',
    'soda',
  ]

I pass these to my Handlebars template and I do this:

{{#each food as |food|}}
  {{food}}
  {{#each drinks as |drink|}}
    {{drink}}
  {{/each}}
{{/each}}

But it only prints out

taco burrito hamburger

I would expect the output to be something like this (separated into newlines for legibility)

taco
water
milk
soda
burrito
water
milk
soda
hamburger
water
milk
soda

It appears to completely ignore the nested each loop. What is wrong here?

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370

1 Answers1

7

This is a context problem. The inner each is in the context of the outer one which doesn't have drinks defined.

Here is an example of a working template;

{{#each food as |food|}}
  {{food}}
  {{#each ../drinks as |drink|}}
    {{drink}}
  {{/each}}
{{/each}}

Here, we use ../ to backtrack to the parent context, where drinks is defined.

Shadow
  • 8,749
  • 4
  • 47
  • 57