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

Strange context change behavior when custom helper inside #each (v4.0.5) #1135

Closed
zordius opened this issue Nov 24, 2015 · 3 comments
Closed
Labels

Comments

@zordius
Copy link

zordius commented Nov 24, 2015

Please refer to this jsfiddle: http://jsfiddle.net/7ywsdydr/ .

Base on #1028 , when a block customhelper execute options.fn(this) , the context stack should not be pushed.

Here is test 1 to confirm the behavior:

Not inside #each:
 1.IF: {{#if true}}{{name}}-{{../name}}-{{../../name}}-{{../../../name}}{{/if}}
 2.MYIF: {{#myif true}}{{name}}={{../name}}={{../../name}}={{../../../name}}{{/myif}}
 3.MYWITH: {{#mywith true}}{{name}}~{{../name}}~{{../../name}}~{{../../../name}}{{/mywith}}

The input data is:

{
    "name": "John",
    "array": [1]
}

The test 1 output is:

Not inside #each:
 1.IF: John---
 2.MYIF: John===
 3.MYWITH: ~John~~

Compare output 1.IF and 2.MYIF we can know the context is not changed when inside the {{#if}} or {{#myif}} block. The {{#myif}} helper is defined as:

Handlebars.registerHelper('myif', function(conditional, options) {
    if (conditional) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});

Which almost same with {{#if}}. But, when this test be placed into a {{#each}} :

Inside #each:
{{#each array}}
 1.IF: {{#if true}}{{name}}-{{../name}}-{{../../name}}-{{../../../name}}{{/if}}
 2.MYIF: {{#myif true}}{{name}}={{../name}}={{../../name}}={{../../../name}}{{/myif}}
 3.MYWITH: {{#mywith true}}{{name}}~{{../name}}~{{../../name}}~{{../../../name}}{{/mywith}}
{{/each}}

Output will be:

Inside #each:
 1.IF: -John--
 2.MYIF: ==John=
 3.MYWITH: ~~John~

Note on difference between 1.IF and 2.MYIF . The issue is: when {{#if}} do not change the context, in the same time {{#myif}} changed the context. Why?

@kpdecker kpdecker added the bug label Nov 24, 2015
@kpdecker
Copy link
Collaborator

Thanks for the report. Something certainly seems wrong here. Will have to take a look.

@kpdecker
Copy link
Collaborator

The issue here is the use of 1 as the context. At some point, I suspect through .call somewhere causes the value to be boxed to Number: 1, which fails the strict equality check. It's an unsafe edge case to iterate on literals in the manner, but the fix (changing !== to !=) is simple enough and seems like it's ok to make.

@zordius
Copy link
Author

zordius commented Dec 14, 2015

Thank you for the fix!

lawnsea pushed a commit to lawnsea/handlebars.js that referenced this issue Nov 9, 2016
lawnsea pushed a commit that referenced this issue Nov 11, 2016
nknapp pushed a commit that referenced this issue Mar 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants