Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template: inheritance: fix for two partials with different subs values #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ var Hogan = {};
return val;
}

function objectAssign(to, source) {
for (var key in source) to[key] = source[key];
return to;
}

function createSpecializedPartial(instance, subs, partials, stackSubs, stackPartials, stackText) {
function PartialTemplate() {};
PartialTemplate.prototype = instance;
Expand All @@ -290,22 +295,22 @@ var Hogan = {};
partial.buf = '';

stackSubs = stackSubs || {};
partial.stackSubs = stackSubs;
partial.stackSubs = objectAssign({}, stackSubs);
partial.subsText = stackText;
for (key in subs) {
if (!stackSubs[key]) stackSubs[key] = subs[key];
if (!partial.stackSubs[key]) partial.stackSubs[key] = subs[key];
}
for (key in stackSubs) {
partial.subs[key] = stackSubs[key];
for (key in partial.stackSubs) {
partial.subs[key] = partial.stackSubs[key];
}

stackPartials = stackPartials || {};
partial.stackPartials = stackPartials;
partial.stackPartials = objectAssign({}, stackPartials);
for (key in partials) {
if (!stackPartials[key]) stackPartials[key] = partials[key];
if (!partial.stackPartials[key]) partial.stackPartials[key] = partials[key];
}
for (key in stackPartials) {
partial.partials[key] = stackPartials[key];
for (key in partial.stackPartials) {
partial.partials[key] = partial.stackPartials[key];
}

return partial;
Expand Down
34 changes: 34 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,40 @@ test("Recursion in inherited templates", function() {
is(s, "override override override don't recurse", "matches expected recursive output");
});

test('Using a inherited partials twice with different sub values should have a different output', function() {
is(
Hogan
.compile('{{<a}}{{/a}}')
.render({}, {
a: '{{<b}}{{$i}} Y {{/i}}{{/b}} {{<b}}{{$i}} Z {{/i}}{{/b}}',
b: '{{$i}}{{/i}}'
}),
' Y Z ',
'must return different values'
);
});

test('Lambdas in deep partials with the same name', function() {
is(
Hogan
.compile('{{<a}}{{/a}}')
.render({
l: function() {
return function(text) {
return text.toLowerCase();
}
}
}, {
a: '{{$g}}{{/g}}' +
'{{<b}}{{$i}} {{#l}}Z{{/l}} {{/i}}{{/b}} ' +
'{{<b}}{{$i}} {{#l}}X{{/l}} {{/i}}{{/b}}',
b: '{{$i}}{{/i}}'
}),
' z x ',
'lambdas should have different text provided'
);
});

test("Cache contains old partials instances", function() {
var tests = [{
template: "{{<parent}}{{$a}}c{{/a}}{{/parent}}",
Expand Down