Handlebars allows for template reuse through partials. Partials are normal Handlebars templates that may be called directly by other templates.
Handlebars.registerPartial
.
Handlebars.registerPartial('myPartial', '{{name}}')
myPartial
partial. Partials may be precompiled and the precompiled template passed into the second parameter.
{{> myPartial }}
myPartial
. When the partial executes, it will be run under the current execution context.
{{> (whichPartial) }}
whichPartial
and then render the partial whose name is returned by this function.
whichPartial
must be a function. If a simple variable has the partial name, it's possible to resolve it via the lookup
helper.
{{> (lookup . 'myVariable') }}
{{> myPartial myOtherContext }}
{{> myPartial parameter=value }}
parameter
to value
when the partial runs.
{{> myPartial name=../name }}
{{#> myPartial }} Failover content {{/myPartial}}
Failover content
if the myPartial
partial is not registered.
@partial-block
. A template of
{{#> layout }} My Content {{/layout}}
layout
partial containing
Site Content
{{> @partial-block }}
Site Content My Content
{{#each children as |child|}} {{#> childEntry}} {{child.value}} {{/childEntry}} {{/each}}
child.value
from this template, not the partial.
inline
decorator.
{{#*inline "myPartial"}} My Content {{/inline}} {{#each children}} {{> myPartial}} {{/each}}
myPartial
partial for each child.
{{#> layout}} {{#*inline "nav"}} My Nav {{/inline}} {{#*inline "content"}} My Content {{/inline}} {{/layout}}
layout
partial may be:
<div class="nav"> {{> nav}} </div> <div class="content"> {{> content}} </div>