Merge branch 'master' of github.com:wycats/handlebars.js

This commit is contained in:
tomhuda
2012-07-12 10:07:10 -07:00
12 changed files with 182 additions and 33 deletions
+16 -2
View File
@@ -56,13 +56,27 @@ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
}
});
Handlebars.K = function() {};
Handlebars.createFrame = Object.create || function(object) {
Handlebars.K.prototype = object;
var obj = new Handlebars.K();
Handlebars.K.prototype = null;
return obj;
};
Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var ret = "";
var ret = "", data;
if (options.data) {
data = Handlebars.createFrame(options.data);
}
if(context && context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]);
if (data) { data.index = i; }
ret = ret + fn(context[i], { data: data });
}
} else {
ret = inverse(this);
+5
View File
@@ -93,6 +93,11 @@ var Handlebars = require('./base');
this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
};
Handlebars.AST.DataNode = function(id) {
this.type = "DATA";
this.id = id;
};
Handlebars.AST.StringNode = function(string) {
this.type = "STRING";
this.string = string;
+26 -9
View File
@@ -210,17 +210,17 @@ Handlebars.JavaScriptCompiler = function() {};
simpleMustache: function(mustache, program, inverse) {
var id = mustache.id;
this.addDepth(id.depth);
this.opcode('getContext', id.depth);
if (id.parts.length) {
this.opcode('lookupOnContext', id.parts[0]);
for(var i=1, l=id.parts.length; i<l; i++) {
this.opcode('lookup', id.parts[i]);
}
if (id.type === 'DATA') {
this.DATA(id);
} else if (id.parts.length) {
this.ID(id);
} else {
// Simplified ID for `this`
this.addDepth(id.depth);
this.opcode('getContext', id.depth);
this.opcode('pushContext');
}
this.opcode('resolvePossibleLambda');
},
@@ -247,6 +247,11 @@ Handlebars.JavaScriptCompiler = function() {};
}
},
DATA: function(data) {
this.options.data = true;
this.opcode('lookupData', data.id);
},
STRING: function(string) {
this.opcode('pushString', string.string);
},
@@ -271,6 +276,7 @@ Handlebars.JavaScriptCompiler = function() {};
},
addDepth: function(depth) {
if(isNaN(depth)) { throw new Error("EWOT"); }
if(depth === 0) { return; }
if(!this.depths[depth]) {
@@ -437,7 +443,8 @@ Handlebars.JavaScriptCompiler = function() {};
if (!this.isChild) {
var namespace = this.namespace;
var copies = "helpers = helpers || " + namespace + ".helpers;";
if(this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
if (this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
if (this.options.data) { copies = copies + " data = data || {};"; }
out.push(copies);
} else {
out.push('');
@@ -646,6 +653,16 @@ Handlebars.JavaScriptCompiler = function() {};
});
},
// [lookupData]
//
// On stack, before: ...
// On stack, after: data[id], ...
//
// Push the result of looking up `id` on the current data
lookupData: function(id) {
this.pushStack(this.nameLookup('data', id, 'data'));
},
// [pushStringParam]
//
// On stack, before: ...
+4
View File
@@ -111,6 +111,10 @@ Handlebars.PrintVisitor.prototype.ID = function(id) {
}
};
Handlebars.PrintVisitor.prototype.DATA = function(data) {
return "@" + data.id;
};
Handlebars.PrintVisitor.prototype.content = function(content) {
return this.pad("CONTENT[ '" + content.string + "' ]");
};