Fix for #1252: Using @partial-block twice in a template not possible

Fixes #1252
- This fix treats partial-blocks more like closures and uses the closure-context of
  the "invokePartial"-function to store the @partial-block for the partial.
- Adds a tes for the fix
This commit is contained in:
Nils Knappmeier
2016-12-31 00:32:03 +01:00
committed by Nils Knappmeier
parent 01b0f656bb
commit 5a164d0ca5
2 changed files with 21 additions and 10 deletions
+14 -10
View File
@@ -210,12 +210,7 @@ export function wrapProgram(container, i, fn, data, declaredBlockParams, blockPa
export function resolvePartial(partial, context, options) {
if (!partial) {
if (options.name === '@partial-block') {
let data = options.data;
while (data['partial-block'] === noop) {
data = data._parent;
}
partial = data['partial-block'];
data['partial-block'] = noop;
partial = options.data['partial-block'];
} else {
partial = options.partials[options.name];
}
@@ -228,6 +223,8 @@ export function resolvePartial(partial, context, options) {
}
export function invokePartial(partial, context, options) {
// Use the current closure context to save the partial-block if this partial
const currentPartialBlock = options.data && options.data['partial-block'];
options.partial = true;
if (options.ids) {
options.data.contextPath = options.ids[0] || options.data.contextPath;
@@ -236,10 +233,17 @@ export function invokePartial(partial, context, options) {
let partialBlock;
if (options.fn && options.fn !== noop) {
options.data = createFrame(options.data);
partialBlock = options.data['partial-block'] = options.fn;
if (partialBlock.partials) {
options.partials = Utils.extend({}, options.partials, partialBlock.partials);
// Wrapper function to get access to currentPartialBlock from the closure
let fn = options.fn;
partialBlock = options.data['partial-block'] = function partialBlockWrapper(context, options) {
// Restore the partial-block from the closure for the execution of the block
// i.e. the part inside the block of the partial call.
options.data = createFrame(options.data);
options.data['partial-block'] = currentPartialBlock;
return fn(context, options);
};
if (fn.partials) {
options.partials = Utils.extend({}, options.partials, fn.partials);
}
}
+7
View File
@@ -249,6 +249,13 @@ describe('partials', function() {
true,
'success');
});
it('should be able to render the partial-block twice', function() {
shouldCompileToWithPartials(
'{{#> dude}}success{{/dude}}',
[{}, {}, {dude: '{{> @partial-block }} {{> @partial-block }}'}],
true,
'success success');
});
it('should render block from partial with context', function() {
shouldCompileToWithPartials(
'{{#> dude}}{{value}}{{/dude}}',