diff --git a/lib/handlebars.js b/lib/handlebars.js index afa80134..ffcdb0e3 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -5,6 +5,7 @@ var Handlebars = { if (Handlebars.compilerCache[string] == null) { var fnBody = Handlebars.compileFunctionBody(string); var fn = new Function("context", "fallback", "Handlebars", fnBody); + console.log(fn); Handlebars.compilerCache[string] = function(context, fallback) { return fn(context, fallback, Handlebars); }; } @@ -79,7 +80,9 @@ var Handlebars = { return compiled; }, - evalExpression: function(path, context, stack) { + evalExpression: function(path, context, stack, fallback) { + fallback = fallback || {}; + var parsedPath = Handlebars.parsePath(path); var depth = parsedPath[0]; var parts = parsedPath[1]; @@ -89,10 +92,14 @@ var Handlebars = { context = stack[stack.length - depth]; } - for (var i = 0; i < parts.length && context !== undefined; i++) { + for (var i = 0; i < parts.length && typeof context !== "undefined"; i++) { context = context[parts[i]]; } - + + if (parts.length == 1 && typeof context === "undefined") { + return fallback[parts[0]]; + } + return context; }, @@ -350,17 +357,12 @@ Handlebars.Compiler.prototype = { }, lookupFor: function(param) { - var parsed = Handlebars.parsePath(param); - var depth = parsed[0]; - var parts = parsed[1]; - - if (depth > 0 || parts.length > 1) { - return "(Handlebars.evalExpression('" + param + "', context, stack))"; - } else if (parts.length == 1) { - return "(!Handlebars.isEmpty(context['" + parts[0] + "']) ? context['" + parts[0] + "'] : fallback['" + parts[0] + "'])"; - } else { - return "(!Handlebars.isEmpty(context) ? context : null)"; - } + if (typeof param === "undefined") { + return "context"; + } + else { + return "(Handlebars.evalExpression('" + param + "', context, stack, fallback))"; + } }, compileToEndOfBlock: function(mustache) { diff --git a/test/handlebars.js b/test/handlebars.js index a589ee55..0feb5592 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -100,13 +100,13 @@ test("nested paths with empty string value", function() { }); test("bad idea nested paths", function() { + var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; shouldThrow(function() { - Handlebars.compile("{{#goodbyes}}{{../name/../name}}{{/goodbyes}}"); + Handlebars.compile("{{#goodbyes}}{{../name/../name}}{{/goodbyes}}")(hash); }, Handlebars.Exception, "Cannot jump (..) into previous context after moving into a context."); var string = "{{#goodbyes}}{{.././world}} {{/goodbyes}}"; - var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"}; shouldCompileTo(string, hash, "world world world ", "Same context (.) is ignored in paths"); });