Add a comment and reorganize a few things

This commit is contained in:
wycats
2010-08-07 19:27:10 -07:00
parent a9dfa3605c
commit c907e80d15
+34 -29
View File
@@ -35,6 +35,8 @@ Handlebars = {
// Returns a two element array containing the numbers of contexts to back up the stack and
// the properties to dig into on the current context
//
// For example, if the path is "../../alan/name", the result will be [2, ["alan", "name"]].
parsePath: function(path) {
if (path == null) {
return [0, []];
@@ -44,7 +46,7 @@ Handlebars = {
var readDepth = false;
var depth = 0;
var dig = [];
for (var i = 0; i < parts.length; i++) {
for (var i = 0, j = parts.length; i < j; i++) {
switch(parts[i]) {
case "..":
if (readDepth) {
@@ -165,6 +167,35 @@ Handlebars.Compiler.prototype = {
}
},
addBlock: function(mustache, param, parts) {
// 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
var result = compiler.compile(function(compiler) {
if(compiler.peek(mustache.length + 5) === "{{/" + mustache + "}}") {
compiler.getChar(mustache.length + 5);
return true;
}
});
// move the pointer forward the amount of characters handled by the sub-compiler
this.pointer += compiler.pointer + 1;
// each function made internally needs a unique IDs. These are locals, so they
// don't need to be globally unique, just per compiler
var fnId = "fn" + this.pointer.toString();
this.fn += "var proxy = Handlebars.buildContext(" + param + ", stack);";
this.fn += "var " + fnId + " = function(context) {" + result + "}; ";
this.fn += "lookup = " + this.lookupFor(mustache) + "; ";
this.fn += "if(Handlebars.isFunction(lookup)) out = out + lookup.call(proxy, " + fnId + "); ";
this.fn += "else if(typeof lookup !== 'undefined') out = out + Handlebars.helperMissing.call(proxy, lookup, " + fnId + "); ";
this.fn += "stack.pop();";
this.openBlock = false;
},
parseMustache: function() {
var chr, part, mustache, param;
@@ -193,36 +224,10 @@ 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
var result = compiler.compile(function(compiler) {
if(compiler.peek(mustache.length + 5) === "{{/" + mustache + "}}") {
compiler.getChar(mustache.length + 5);
return true;
}
});
// move the pointer forward the amount of characters handled by the sub-compiler
this.pointer += compiler.pointer + 1;
// each function made internally needs a unique IDs. These are locals, so they
// don't need to be globally unique, just per compiler
var fnId = "fn" + this.pointer.toString();
this.fn += "var proxy = Handlebars.buildContext(" + param + ", stack);";
this.fn += "var " + fnId + " = function(context) {" + result + "}; ";
this.fn += "lookup = " + this.lookupFor(mustache) + "; ";
this.fn += "if(Handlebars.isFunction(lookup)) out = out + lookup.call(proxy, " + fnId + "); ";
this.fn += "else if(typeof lookup !== 'undefined') out = out + Handlebars.helperMissing.call(proxy, lookup, " + fnId + "); ";
this.fn += "stack.pop();";
this.openBlock = false;
this.addBlock(mustache, param, parts)
return;
} else {
this.addExpression(mustache);
return;
return this.addExpression(mustache);
}
} else if(this.comment) {
;