Add support for @data variables
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -96,7 +96,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
this.children[guid] = result;
|
||||
|
||||
for(var i=0, l=result.depths.list.length; i<l; i++) {
|
||||
var depth = result.depths.list[i];
|
||||
depth = result.depths.list[i];
|
||||
|
||||
if(depth < 2) { continue; }
|
||||
else { this.addDepth(depth - 1); }
|
||||
@@ -210,10 +210,14 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
simpleMustache: function(mustache, program, inverse) {
|
||||
var id = mustache.id;
|
||||
|
||||
this.addDepth(id.depth);
|
||||
this.opcode('getContext', id.depth);
|
||||
if (id.type === 'ID') {
|
||||
this.addDepth(id.depth);
|
||||
this.opcode('getContext', id.depth);
|
||||
}
|
||||
|
||||
if (id.parts.length) {
|
||||
if (id.type === 'DATA') {
|
||||
this.opcode('lookupData', id.id);
|
||||
} else 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]);
|
||||
@@ -247,6 +251,10 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
}
|
||||
},
|
||||
|
||||
DATA: function(data) {
|
||||
this.opcode('lookupData', data.id);
|
||||
},
|
||||
|
||||
STRING: function(string) {
|
||||
this.opcode('pushString', string.string);
|
||||
},
|
||||
@@ -271,6 +279,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
},
|
||||
|
||||
addDepth: function(depth) {
|
||||
if(isNaN(depth)) { throw new Error("EWOT"); }
|
||||
if(depth === 0) { return; }
|
||||
|
||||
if(!this.depths[depth]) {
|
||||
@@ -646,6 +655,10 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
});
|
||||
},
|
||||
|
||||
lookupData: function(id) {
|
||||
this.pushStack(this.nameLookup('data', id, 'data'));
|
||||
},
|
||||
|
||||
// [pushStringParam]
|
||||
//
|
||||
// On stack, before: ...
|
||||
@@ -831,7 +844,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
||||
var programParams = [child.index, child.name, "data"];
|
||||
|
||||
for(var i=0, l = depths.length; i<l; i++) {
|
||||
var depth = depths[i];
|
||||
depth = depths[i];
|
||||
|
||||
if(depth === 1) { programParams.push("depth0"); }
|
||||
else { programParams.push("depth" + (depth - 1)); }
|
||||
|
||||
@@ -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 + "' ]");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user