0

How can I add a dynamic class name in an #each block to my Handlebars template, where the class name isn't part of the block scope?

<div class="{{className}}>...</div>

{{#each items}}
    <div class="{{className}}">
        ...
    </div>
{{/each}}

The first <div> will see the class name, whereas the second one in the #each block doesn't, because it is now looking for a className in items.
Is it possible to see outside of the items scope in the #each block?

Jan Swart
  • 6,761
  • 10
  • 36
  • 45

2 Answers2

1

You need to go back one scope (or possibly more), using ../ to access a global variable. In your case if your pass i className as one attribute and ìtems`as another, your code should look like this:

{{#each items}}
   <div class="{{../className}}">
    ...
   </div>
{{/each}}
jasper
  • 355
  • 3
  • 13
0

@root.classname will help on any depth of scope.

Alex.Default
  • 236
  • 3
  • 16