Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong context when iterating an array with undefined inside a conditional block helper #1762

Open
aleen42 opened this issue Jun 29, 2021 · 0 comments

Comments

@aleen42
Copy link

aleen42 commented Jun 29, 2021

Recently I found that I have missed my context with the following snippet:

const Handlebars = require('handlebars');

Handlebars.compile('{{#each arr}}'
                   + '{{#if 1}}{{@index}}: {{../foo}}|{{/if}}'
                   + '{{/each}}')({
    arr : [1, 2, void 0, 4],
    foo : 'foo',
}); // => "0: foo|1: foo|2: |3: foo|"

Handlebars.compile('{{#each arr}}'
                   + '{{#if 0}}{{^}}{{@index}}: {{../foo}}|{{/if}}'
                   + '{{/each}}')({
    arr : [1, 2, void 0, 4],
    foo : 'foo',
}); // => "0: foo|1: foo|2: |3: foo|"

The problem only occurs inside a conditional block helper:

Handlebars.compile('{{#each arr}}'
                   + '{{@index}}: {{../foo}}|'
                   + '{{/each}}')({
    arr : [1, 2, void 0, 4],
    foo : 'foo',
}); // => "0: foo|1: foo|2: foo|3: foo|"

It is a common case that a conditional block helper should not create a new context, but actually, it has wrapped an empty context when iterating an array with undefined inside it.

@aleen42 aleen42 changed the title Wrong context when iterating an array with undefined inside a conditional block helper Wrong context when iterating an array with undefined inside a conditional block helper Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants