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

adding support for leading dot. adding use of prototypical inheritance for Stack's substitutions and partials #237

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 16 additions & 10 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var Hogan = {};
if (partial.subs) {
// Make sure we consider parent template now
if (!partials.stackText) partials.stackText = {};
for (key in partial.subs) {
for (var key in partial.subs) {
if (!partials.stackText[key]) {
partials.stackText[key] = (this.activeSub !== undefined && partials.stackText[this.activeSub]) ? partials.stackText[this.activeSub] : this.text;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ var Hogan = {};
pass = !!val;

if (!inverted && pass && ctx) {
ctx.push((typeof val == 'object') ? val : ctx[ctx.length - 1]);
ctx.push(val);
}

return pass;
Expand All @@ -137,13 +137,11 @@ var Hogan = {};
d: function(key, ctx, partials, returnFound) {
var found,
names = key.split('.'),
val = this.f(names[0], ctx, partials, returnFound),
val = names[0] === '' ? ctx[ctx.length - 1] : this.f(names[0], ctx, partials, returnFound),
doModelGet = this.options.modelGet,
cx = null;

if (key === '.' && isArray(ctx[ctx.length - 2])) {
val = ctx[ctx.length - 1];
} else {
if (key !== '.') {
for (var i = 1; i < names.length; i++) {
found = findInScope(names[i], val, doModelGet);
if (found !== undefined) {
Expand Down Expand Up @@ -279,17 +277,25 @@ var Hogan = {};
}

function createSpecializedPartial(instance, subs, partials, stackSubs, stackPartials, stackText) {
function PartialTemplate() {};
function PartialTemplate() {}
PartialTemplate.prototype = instance;
function Substitutions() {};

function Substitutions() {}
Substitutions.prototype = instance.subs;

function StackSubstitutions() {}
StackSubstitutions.prototype = stackSubs || {};

function StackPartials() {}
StackPartials.prototype = stackPartials || {};

var key;
var partial = new PartialTemplate();
partial.subs = new Substitutions();
partial.subsText = {}; //hehe. substext.
partial.buf = '';

stackSubs = stackSubs || {};
stackSubs = new StackSubstitutions();
partial.stackSubs = stackSubs;
partial.subsText = stackText;
for (key in subs) {
Expand All @@ -299,7 +305,7 @@ var Hogan = {};
partial.subs[key] = stackSubs[key];
}

stackPartials = stackPartials || {};
stackPartials = new StackPartials();
partial.stackPartials = stackPartials;
for (key in partials) {
if (!stackPartials[key]) stackPartials[key] = partials[key];
Expand Down
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,13 @@ test("Dotted Names", function() {
is(s, '"Joe" == "Joe"', "dotted names work");
});

test("Leading Dot", function() {
var text = "{{#items}}{{.title}}{{/items}}";
var t = Hogan.compile(text);
var s = t.render({items: [{title: 'a'}, {title: 'b'}, {}], title: 'ha'});
is(s, "ab", "leading dot forces lookup on top context");
});

test("Implicit Iterator", function() {
var text = '{{#stuff}} {{.}} {{/stuff}}';
var t = Hogan.compile(text);
Expand Down Expand Up @@ -1035,6 +1042,18 @@ test("Lambdas work in multi-level inheritance", function() {
is(child, 'changed c - changed p - changed o - changed g', 'should be changed child value');
});

test("Substitutions used in call for one partial template should not affect other during multi-level inheritance", function() {
var subPartial = Hogan.compile("Haloha!{{$replace}}replace me{{/replace}}");
var partial = Hogan.compile("{{>subPartial}} {{<subPartial}}{{$replace}}replaced1{{/replace}}{{/subPartial}} {{<subPartial}}{{$replace}}replaced2{{/replace}}{{/subPartial}}");
var t = Hogan.compile("{{>partial}}");

var s = t.render({}, {
subPartial: subPartial,
partial: partial
});
is(s, "Haloha!replace me Haloha!replaced1 Haloha!replaced2", "Substitutions are correctly handled during multi-level inheritance");
});

/* Safety tests */

test("Updates object state", function() {
Expand Down