Got basic inverted sections working.

This commit is contained in:
Alan Johnson
2010-08-18 20:57:02 -04:00
parent 2a112fe07f
commit a9c875e0a9
2 changed files with 21 additions and 11 deletions
+20 -10
View File
@@ -110,16 +110,28 @@ Handlebars = {
},
isEmpty: function(value) {
if (typeof object === "undefined") {
if (typeof value === "undefined") {
return true;
} else if (!object) {
} else if (!value) {
return true;
} else if(toString.call(object) === "[object Array]" && object.length == 0) {
} else if(toString.call(value) === "[object Array]" && value.length == 0) {
return true;
} else {
return false;
}
}
},
// Escapes output and converts empty values to empty strings
filterOutput: function(value, escape) {
if (Handlebars.isEmpty(value)) {
return "";
} else if (escape) {
return Handlebars.escapeExpression(value);
} else {
return value;
}
},
}
Handlebars.Compiler = function(string) {
@@ -208,13 +220,10 @@ Handlebars.Compiler.prototype = {
addExpression: function(mustache) {
var expr = this.lookupFor(mustache);
var escapeCall = "(";
if (this.escaped) {
escapeCall = "Handlebars.escapeExpression(";
}
this.fn += "if (Handlebars.isFunction(" + expr + ")) out = out + " + escapeCall + expr + ".call(Handlebars.buildContext(context, stack))); ";
this.fn += "else if(typeof " + expr + "!== 'undefined') out = out + " + escapeCall + expr + ");";
this.fn += "if (Handlebars.isFunction(" + expr + ")) out = out + Handlebars.filterOutput(" + expr + ".call(Handlebars.buildContext(context, stack)), " + this.escaped + "); ";
this.fn += "else if(typeof " + expr + "!== 'undefined') out = out + Handlebars.filterOutput(" + expr + ", " + this.escaped + ");";
console.log(this.fn);
},
addInvertedSection: function(mustache) {
@@ -228,6 +237,7 @@ Handlebars.Compiler.prototype = {
this.fn += "var " + fnId + " = function(context) {" + result + "}; ";
this.fn += "lookup = " + this.lookupFor(mustache) + "; ";
this.fn += "if(Handlebars.isFunction(lookup) && Handlebars.isEmpty(lookup())) out = out + " + fnId + "(proxy);";
this.fn += "else if (Handlebars.isEmpty(lookup)) out = out + " + fnId + "(proxy);";
console.log(this.fn);
this.openBlock = false;
+1 -1
View File
@@ -107,7 +107,7 @@ test("inverted section with empty set", function() {
});
test("inverted section using result of function call", function() {
var string = "{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}";
var string = "{{goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}";
var hash = {goodbyes: function() { return false; }}
shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when result of function in expression is false.");
});