Skip to content

Commit

Permalink
adding support for leading dot. see mustache/spec#52
Browse files Browse the repository at this point in the history
  • Loading branch information
fedarovs authored and fedarovs committed Dec 17, 2015
1 parent 7e340e9 commit 209433b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/template.js
Original file line number Diff line number Diff line change
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
7 changes: 7 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

0 comments on commit 209433b

Please sign in to comment.