Skip to content

Commit

Permalink
Fixes issue #194.
Browse files Browse the repository at this point in the history
Allow sections extensions with outer sections to access the properties
of outer contexts. This allows for section extensions to behave like
hogan.js does internally when looking up properties directly in
templates.
  • Loading branch information
torgeir committed Jun 19, 2014
1 parent 9c4584a commit 6e6d69c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ var Hogan = {};
},

// higher order templates
ls: function(func, cx, partials, text, tags) {
ls: function(func, cx, ctx, partials, text, tags) {
var oldTags = this.options.delimiters;

this.options.delimiters = tags;
this.b(this.ct(coerceToString(func.call(cx, text)), cx, partials));
this.b(this.ct(coerceToString(func.call(cx, text, ctx)), cx, partials));
this.options.delimiters = oldTags;

return false;
Expand Down Expand Up @@ -230,7 +230,7 @@ var Hogan = {};
return true;
} else {
textSource = (this.activeSub && this.subsText && this.subsText[this.activeSub]) ? this.subsText[this.activeSub] : this.text;
return this.ls(result, cx, partials, textSource.substring(start, end), tags);
return this.ls(result, cx, ctx, partials, textSource.substring(start, end), tags);
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,25 @@ test("Section Extensions In Higher Order Sections", function() {
is(s, "Testbarqux", "unprocessed test");
});

test("Section Extension With Higher Order Sections Access Outer Context ", function() {
var text = "{{#inner}}{{#extension}}{{outerValue}}{{/extension}}{{/inner}}"
var t = Hogan.compile(text);
var context = {
outerValue: "Outer value",
inner: {
innerValue: "Inner value"
},
extension: function () {
return function (tmpl, ctx) {
var key = /{{(.*)}}/.exec(tmpl)[1];
return ctx[0][key];
}
}
};
var s = t.render(context);
is(s, "Outer value", "unprocessed test");
});

test("Section Extensions In Lambda Replace Variable", function() {
var text = "Test{{foo}}";
var options = {sectionTags:[{o:'_baz', c:'baz'}]};
Expand Down

0 comments on commit 6e6d69c

Please sign in to comment.