helpers / handlebars-helpers

188 handlebars helpers in ~20 categories. Can be used with Assemble, Ghost, YUI, express.js etc.
http://assemble.io/helpers/
MIT License
2.22k stars 364 forks source link

@key value incorrect when using {{isnt}} helper in an {{each}} loop #262

Open blueowl0708 opened 7 years ago

blueowl0708 commented 7 years ago

version

Gulp with Assemble 0.21.0 + Handlebars-Helpers 0.7.7 (tested in latest versions of both)

description

FYI I've moved this over from the assemble repo at the request of @jonschlinkert.


I'm migrating a site from grunt + assemble to gulp + assemble and I'm having trouble with the following code which works fine on the old build.

In our hbs file there's a YAML object:

    Subject heading:
        open: open

        Article title 1:
            description: Description 1
            url: /url1

        Article title 2:
            description: Description 2
            url: /url2

        Article title 3:
            description: Description 3
            url: /url3

We pass this into a partial for processing

{{#with sub-categories}}
    {{> inc-subcategory-expanders}}
{{/with}}

The partial looks like this:

<div>
    {{#each this}}
        <div {{#if this.open}}class="is-open"{{/if}}>
            <h3 id="{{ dashcase @key }}">{{@key}}</h3>
            <ul id="content-{{ dashcase @key }}">
                {{#each this}}
                    {{#isnt @key "open"}}
                    <li>
                        <a href="{{this.url}}">
                            <h4>{{@key}}</h4>
                            <p>{{this.description}}</p>
                        </a>
                    </li>
                    {{/isnt}}
                {{/each}}
            </ul>
        </div>
    {{/each}}
</div>

And this is the output - the article title is always 'Article title 3'

<div>
    <div class="is-open">
        <h3 id="subject-heading">Subject heading</h3>
        <ul id="content-subject-heading">
            <li>
                <a href="/url1">
                    <h4>Article title 3</h4>
                    <p>Description 1</p>
                </a>
            </li>
            <li>
                <a href="/url2">
                    <h4>Article title 3</h4>
                    <p>Description 2</p>
                </a>
            </li>
            <li>
                <a href="/url3">
                    <h4>Article title 3</h4>
                    <p>Description 3</p>
                </a>
            </li>
        </ul>
    </div>
</div>

What I'd expect (and what I get in the grunt assemble build) is:

<div>
    <div class="is-open">
        <h3 id="subject-heading">Subject heading</h3>
        <ul id="content-subject-heading">
            <li>
                <a href="/url1">
                    <h4>Article title 1</h4>
                    <p>Description 1</p>
                </a>
            </li>
            <li>
                <a href="/url2">
                    <h4>Article title 2</h4>
                    <p>Description 2</p>
                </a>
            </li>
            <li>
                <a href="/url3">
                    <h4>Article title 3</h4>
                    <p>Description 3</p>
                </a>
            </li>
        </ul>
    </div>
</div>

{{#isnt @key "open"}} seems to be causing the problem. If I remove this from the second loop, all titles, urls and the description are ok, but then I get 'open' as list item which I don't want.

This may be a case of having to restructure the data in the hbs file, but there's a few so I'd like to avoid that if possible.

shalvah commented 5 years ago

This may be related. The context changes within these helpers. Quite unexpected.: https://github.com/helpers/handlebars-helpers/issues/309#issuecomment-392103700

rgwebcode commented 3 years ago

~~Did anyone ever find a solution for this? I also cannot seem to use {{is @key data.user.status}} some text {{/is}} for example. There's no error thrown, but the text doesn't display correctly either.~~

Scratch this, I forgot to reference the global variable with ../data.user.status, then the comparison worked just fine.