handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

Unexpected behaviour when using {{#with}} and {{#each}}

Open theangrydev opened this issue 3 years ago • 0 comments

I have an example of some unexpected behaviour when using {{#with}} and {{#each}}

Good example:

{{#each (array 'a' 'b' 'c')}}
{{this}}
{{/each}}

Renders:

a
b
c

Bad example:

{{#with (array 'a' 'b' 'c') as |x|}}
   {{#each x}}
       {{this}}
    {{/each}}
{{/with}}

Prints:

[a, b, c]
[a, b, c]
[a, b, c]

Is this behaviour expected? I would expect the second example to behave the same as the first one

One possible fix could be to introduce a {{@value}} syntax when iterating:

{{#with (array 'a' 'b' 'c') as |x|}}
   {{#each x}}
       {{@value}}
    {{/each}}
{{/with}}

theangrydev avatar Jan 31 '23 13:01 theangrydev