diff options
author | tomhuda <tomhuda@tilde.io> | 2012-07-12 10:07:10 -0700 |
---|---|---|
committer | tomhuda <tomhuda@tilde.io> | 2012-07-12 10:07:10 -0700 |
commit | f79af6bfa3081187907c2615ddc3708915b5e44c (patch) | |
tree | 6dea69aaece417a85ab6445a32671d3119883423 /lib/handlebars | |
parent | 92b6c1401a95a1550524d264b1e5f4494f9a587f (diff) | |
parent | efb1e25690e5c05b1d1662d72cac31f3cb90b67f (diff) | |
download | handlebars.js-f79af6bfa3081187907c2615ddc3708915b5e44c.zip handlebars.js-f79af6bfa3081187907c2615ddc3708915b5e44c.tar.gz handlebars.js-f79af6bfa3081187907c2615ddc3708915b5e44c.tar.bz2 |
Merge branch 'master' of github.com:wycats/handlebars.js
Diffstat (limited to 'lib/handlebars')
-rw-r--r-- | lib/handlebars/base.js | 18 | ||||
-rw-r--r-- | lib/handlebars/compiler/ast.js | 5 | ||||
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 35 | ||||
-rw-r--r-- | lib/handlebars/compiler/printer.js | 4 |
4 files changed, 51 insertions, 11 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 2cae62b..659295c 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -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); diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js index d61377e..25abe0a 100644 --- a/lib/handlebars/compiler/ast.js +++ b/lib/handlebars/compiler/ast.js @@ -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; diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 20a558d..ca59725 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -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: ... diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js index 8157190..7a42a66 100644 --- a/lib/handlebars/compiler/printer.js +++ b/lib/handlebars/compiler/printer.js @@ -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 + "' ]"); }; |