From 135e7c34cdf6bde362ff096506d3c8cb6e06f93c Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Tue, 3 Aug 2010 01:43:35 -0400 Subject: [PATCH] Got more deeply neested context references working and tested. --- lib/handlebars.js | 12 +++++++----- test/handlebars.js | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index 882ba515..13ed02e9 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -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 { diff --git a/test/handlebars.js b/test/handlebars.js index 774d693d..01bdf1d1 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -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!"); });