Search code examples
templatesnestedhandlebars.jspartial

In Handlebars, how can I access a partial's content from within the partial?


From my understanding, the typical use of another partial is this:

{{> anotherPartial }}

Apparently, Handlebars also supports this notation:

{{#> anotherPartial }}
  someContent
{{/ anotherPartial }}

Now, how would I access someContent from within anotherPartial.hbs to output something like this?

<div class="anotherPartial">someContent</div>

Solution

  • The Handlebars document on Partial Blocks specifies that the template passed to the partial "can be executed by the specially named partial, @partial-block. This means that in order to render someContent, your anotherPartial partial would look like the following:

    <div class="anotherPartial">
        {{> @partial-block }}
    </div>
    

    I have created a demo in jsFiddle.