Got accessing data in nested objects working using parent/child syntax.

This commit is contained in:
Alan Johnson
2010-08-03 01:59:39 -04:00
parent 135e7c34cd
commit 5d08ed629d
2 changed files with 12 additions and 4 deletions
+7 -4
View File
@@ -2,7 +2,6 @@ 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);
},
@@ -34,7 +33,6 @@ Handlebars.helperMissing = function(object, fn) {
}
return ret;
}
console.log("HERE");
};
Handlebars.Compiler.prototype = {
@@ -93,11 +91,16 @@ Handlebars.Compiler.prototype = {
var parts = param.split("../");
param = parts[parts.length - 1];
depth = parts.length - 1;
parts = param.split("/");
var paramExpr = "";
for (var i = 0; i < parts.length; i++) {
paramExpr += "['" + parts[i] + "']";
}
if (depth > 0) {
return "( stack[stack.length - " + depth + "]['" + param + "'] )";
return "( stack[stack.length - " + depth + "]" + paramExpr + ")";
} else {
return "( context['" + param + "'] || fallback['" + param + "'] )";
return "( context" + paramExpr + " || fallback " + paramExpr + " )";
}
},
+5
View File
@@ -26,6 +26,11 @@ test("boolean", function() {
"booleans do not show the contents when false");
});
test("nested paths", function() {
shouldCompileTo("Goodbye {{alan/expression}} world!", {alan: {expression: "beautiful"}},
"Goodbye beautiful world!", "Nested paths access nested objects");
});
module("blocks");
test("array", function() {