Skip to content

Commit

Permalink
Walk up data frames for nested @partial-block
Browse files Browse the repository at this point in the history
The root cause of handlebars-lang#1218 is that `invokePartial` creates a stack of data frames
for nested partial blocks, but `resolvePartial` always uses the value at top of
the stack without "popping" it. The result is an infinite recursive loop, as
references to `@partial-block` in the partial at the top of the stack resolve to
itself.

So, walk up the stack of data frames when evaluating. This is accomplished by
1) setting the `partial-block` property to `noop` after use and
2) using `_parent['partial-block']` if `partial-block` is `noop`

Fix handlebars-lang#1218
  • Loading branch information
lawnsea committed Aug 16, 2016
1 parent a6a0e50 commit bbe0a94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ export function wrapProgram(container, i, fn, data, declaredBlockParams, blockPa
export function resolvePartial(partial, context, options) {
if (!partial) {
if (options.name === '@partial-block') {
partial = options.data['partial-block'];
let data = options.data;
while (data['partial-block'] === noop) {
data = data._parent;
}
partial = data['partial-block'];
data['partial-block'] = noop;
} else {
partial = options.partials[options.name];
}
Expand Down
14 changes: 14 additions & 0 deletions spec/partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ describe('partials', function() {
true,
'success');
});
it('should render nested partial blocks', function() {
shouldCompileToWithPartials(
'.template-start.{{#> outer}}{{value}}{{/outer}}.template-end.',
[
{value: 'success'},
{},
{
outer: '.outer-start.{{#> nested}}.outer-partial-block-start.{{> @partial-block}}.outer-partial-block-end.{{/nested}}.outer-end.',
nested: '.nested-start.{{> @partial-block}}.nested-end.'
}
],
true,
'.template-start..outer-start..nested-start..outer-partial-block-start.success.outer-partial-block-end..nested-end..outer-end..template-end.');
});
});

describe('inline partials', function() {
Expand Down

0 comments on commit bbe0a94

Please sign in to comment.