Got more deeply neested context references working and tested.

This commit is contained in:
Alan Johnson
2010-08-03 01:43:35 -04:00
parent 7e036fb952
commit 135e7c34cd
2 changed files with 8 additions and 6 deletions
+7 -5
View File
@@ -2,6 +2,7 @@ Handlebars = {
compile: function(string) {
var compiler = new Handlebars.Compiler(string);
var result = compiler.compile();
console.log(compiler.fn);
return new Function("context", "fallback", "fallback = fallback || {}; var stack = [];" + result);
},
@@ -33,6 +34,7 @@ Handlebars.helperMissing = function(object, fn) {
}
return ret;
}
console.log("HERE");
};
Handlebars.Compiler.prototype = {
@@ -127,6 +129,8 @@ Handlebars.Compiler.prototype = {
this.comment = false;
return;
} else if(this.openBlock) {
// set up the stack before the new compiler starts
this.fn += "stack.push(context);";
var compiler = new Handlebars.Compiler(this.string.slice(this.pointer + 1, -1));
// sub-compile with a custom EOF instruction
@@ -142,16 +146,14 @@ Handlebars.Compiler.prototype = {
// each function made internally needs a unique IDs. These are locals, so they
// don't need to be globally unique, just per compiler
console.log("In block " + mustache);
var fnId = "fn" + this.pointer.toString();
this.fn += "var " + fnId + " = function(context) {" + result + "}; ";
this.fn += "stack.push(context);"
this.fn += "lookup = " + this.lookupFor(mustache) + "; ";
this.fn += "if(Handlebars.isFunction(lookup)) out = out + lookup.call(" + param + ", " + fnId + "); "
this.fn += "else if(typeof lookup !== 'undefined') out = out + Handlebars.helperMissing.call(" + param + ", lookup, " + fnId + "); "
this.fn += "stack.pop(context);"
this.fn += "if(Handlebars.isFunction(lookup)) out = out + lookup.call(" + param + ", " + fnId + "); ";
this.fn += "else if(typeof lookup !== 'undefined') out = out + Handlebars.helperMissing.call(" + param + ", lookup, " + fnId + "); ";
this.fn += "stack.pop();";
this.openBlock = false;
return;
} else {
+1 -1
View File
@@ -52,7 +52,7 @@ test("block with complex lookup", function() {
test("block with deep nested complex lookup", function() {
var string = "{{#outer}}Goodbye {{#inner}}cruel {{../../omg}}{{/inner}}{{/outer}}";
var hash = {omg: "OMG!", outer: { inner: { text: "goodbye" } } };
var hash = {omg: "OMG!", outer: [{ inner: [{ text: "goodbye" }] }] };
shouldCompileTo(string, hash, "Goodbye cruel OMG!");
});