Skip to content

Commit

Permalink
Merge pull request #187 from twitter/null_sections
Browse files Browse the repository at this point in the history
Treat null values as key hits to match Mustache.rb and Hogan 2.0
  • Loading branch information
sayrer committed Jun 17, 2014
2 parents 9e6d760 + 31d07f7 commit a2182a1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ var Hogan = {};
} else {
for (var i = 1; i < names.length; i++) {
found = findInScope(names[i], val, doModelGet);
if (found != null) {
if (found !== undefined) {
cx = val;
val = found;
} else {
Expand Down Expand Up @@ -178,7 +178,7 @@ var Hogan = {};
for (var i = ctx.length - 1; i >= 0; i--) {
v = ctx[i];
val = findInScope(key, v, doModelGet);
if (val != null) {
if (val !== undefined) {
found = true;
break;
}
Expand Down Expand Up @@ -262,11 +262,11 @@ var Hogan = {};

//Find a key in an object
function findInScope(key, scope, doModelGet) {
var val, checkVal;
var val;

if (scope && typeof scope == 'object') {

if (scope[key] != null) {
if (scope[key] !== undefined) {
val = scope[key];

// try lookup with get for backbone or similar model data
Expand Down
30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,36 @@ test("Undefined Return Value From Lambda", function() {
is(s, "abcdef", "deal with undefined return values from lambdas.")
});

test("Sections with null values are treated as key hits", function() {
var text = "{{#obj}}{{#sub}}{{^test}}ok{{/test}}{{/sub}}{{/obj}}";
var t = Hogan.compile(text);
var context = {
obj: {
test: true,
sub: {
test: null
}
}
}
var s = t.render(context);
is(s, "ok");
});

test("Sections with undefined values are treated as key misses", function() {
var text = "{{#obj}}{{#sub}}{{#test}}ok{{/test}}{{/sub}}{{/obj}}";
var t = Hogan.compile(text);
var context = {
obj: {
test: true,
sub: {
test: undefined
}
}
}
var s = t.render(context);
is(s, "ok");
});

test("Section Extensions", function() {
var text = "Test {{_//|__foo}}bar{{/foo}}";
var options = {sectionTags:[{o:'_//|__foo', c:'foo'}]};
Expand Down

0 comments on commit a2182a1

Please sign in to comment.