Got empty blocks working.

This commit is contained in:
Alan Johnson
2010-09-16 08:09:27 -04:00
parent 5b5e686c0d
commit 0f8593f163
2 changed files with 33 additions and 19 deletions
+23 -19
View File
@@ -172,6 +172,7 @@ var Handlebars = {
out = out + lookup.call(context, arg, fn);
}
else if (typeof lookup != 'undefined') {
console.log(fn);
out = out + Handlebars.helperMissing.call(arg, lookup, fn);
}
@@ -262,29 +263,32 @@ Handlebars.Compiler.prototype = {
},
compile: function(endCondition) {
var chr;
while(chr = this.getChar()) {
if(chr === "{" && this.peek() === "{" && !this.mustache) {
this.getChar();
this.parseMustache();
// if we're at the end condition already then we don't have to do any work!
if (!endCondition || !endCondition(this)) {
var chr;
while(chr = this.getChar()) {
if(chr === "{" && this.peek() === "{" && !this.mustache) {
this.getChar();
this.parseMustache();
} else {
if(chr === "\n") {
this.newlines = this.newlines + "\n";
chr = "\\n";
} else if (chr === "\r") {
this.newlines = this.newlines + "\r";
chr = "\\r";
} else {
if(chr === "\n") {
this.newlines = this.newlines + "\n";
chr = "\\n";
} else if (chr === "\r") {
this.newlines = this.newlines + "\r";
chr = "\\r";
}
this.text = this.text + chr;
}
this.text = this.text + chr;
}
if (endCondition && this.peek(5) == "{{^}}") {
this.continueInverted = true;
this.getChar(5);
break;
if (endCondition && this.peek(5) == "{{^}}") {
this.continueInverted = true;
this.getChar(5);
break;
}
else if(endCondition && endCondition(this)) { break };
}
else if(endCondition && endCondition(this)) { break };
}
this.addText();
+10
View File
@@ -138,6 +138,16 @@ test("array", function() {
});
test("empty block", function() {
var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!"
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
shouldCompileTo(string, hash, "cruel world!",
"Arrays iterate over the contents when not empty");
shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!",
"Arrays ignore the contents when empty");
});
test("nested iteration", function() {
});