handlebars.jsassemble

Passing through variable into partial and using an #is helper - Handlebars


I'm trying to pass a variable (tag name) into a Handlebars partial and use an #is block helper on the tag but for some reason it just won't play ball. This is my code:

Call to my partial and passing through the tag name.

{{> nav tagged='page' }}

In the partial itself I do the following (tagged is the variable name passed through):

{{#each tags}}
  {{#is tag tagged}}
    {{#each pages}}
       // Do code here
    {{/each}}
  {{/is}}
{{/each}}

If I just render the tagged variable it displays the variable value as expected so a bit confused as to why its not working.

Thanks.


Solution

  • The issue you have is that the tagged variable is in the parent context but you're trying to reference it within the #each tags loop.

    You can reference the parent context with ../ so the working code would be

    {{#each tags}}
      {{#is tag ../tagged}}
        {{#each pages}}
           // Do code here
        {{/each}}
      {{/is}}
    {{/each}}