0

I'm working on a Handlebars project and trying to use an inline partial. I'm trying to access the key parameter from within a loop.

{{#*inline "myPartial"}}
  <div class="actionOverlay">
    {{#each (lookup items key) }}
      <span class="{{classes}} key-{{key}}" title="{{title}}"></span>
    {{/each}}
  </div>
{{/inline}}

And then invoking this inline partial with {{> myPartial key="0R"}}. The variable items is an array of objects with classes and title as keys.

Unfortunately {{key}} renders as empty string, no doubt because there is no key key in the items objects. I've also tried {{@root.key}} and that doesn't work.

How can I access the partial's parameter from within the loop?

Please note that I'm not trying to use a partial from within a loop, but trying to use a loop in a partial and to access the partial's parameter from within the loop.

JohnK
  • 5,481
  • 6
  • 42
  • 69
  • 1
    You need to step-up a context level. You can use `key-{{../key}}`. – 76484 Nov 10 '20 at 00:13
  • Does this answer your question? [Access a variable outside the scope of a Handlebars.js each loop](https://stackoverflow.com/questions/13645084/access-a-variable-outside-the-scope-of-a-handlebars-js-each-loop) – 76484 Nov 10 '20 at 15:19
  • Thanks, guys! `{{../key}}` did work. I don't know why that worked but `{{@root.key}}` didn't. – JohnK Nov 10 '20 at 16:35
  • 1
    `{{@root.key}}` would work only if `key` was a property of the (root) object you passed to your template function. As `key` is a parameter to the inline partial, it is available within the context in which the partial is executed, but this is not the same as `@root`. – 76484 Nov 10 '20 at 16:41

0 Answers0