diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 80defd8e..1e676ea2 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -7,6 +7,10 @@ if(exports) { Handlebars.Visitor = require("handlebars/jison_ext").Visitor; } +// A Context wraps data, and makes it possible to extract a +// new Context given a path. For instance, if the data +// is { person: { name: "Alan" } }, a Context wrapping +// "Alan" can be extracted by searching for "person/name" Handlebars.Context = function(data, fallback) { this.data = data; this.fallback = fallback; @@ -15,6 +19,7 @@ Handlebars.Context = function(data, fallback) { Handlebars.Context.prototype = { isContext: true, + // Make a shallow copy of the Context clone: function() { var context = new Handlebars.Context; context.data = this.data; @@ -22,9 +27,19 @@ Handlebars.Context.prototype = { return context; }, + // Search for an object inside the Context's data. The + // path parameter is an object with parts + // ("person/name" represented as ["person", "name"]), + // and depth (the amount of levels to go up the stack, + // originally represented as ..). The stack parameter + // is the objects already searched from the root of + // the original Context in order to get to this point. + // + // Return a new Context wrapping the data found in + // the search. evaluate: function(path, stack) { var context = this.clone(); - var depth = path.depth, dig = path.dig, parts = path.parts; + var depth = path.depth, parts = path.parts; if(depth > stack.length) { context.data = null; } else if(depth > 0) { context = stack[stack.length - depth].clone(); } @@ -85,7 +100,7 @@ Handlebars.Runtime.prototype = { mustache: function(mustache) { var idObj = this.accept(mustache.id); - var params = mustache.params; + var params = this.evaluateParams(mustache.params); for(var i=0, l=params.length; i partial }}{{# bar }}part1 {{^}} part2{{> foo }}{{/bar}}zomg" -var string = "foo {{person/name/0}} '{{ join person/name \", \" }}' baz"; +var string = "foo {{#list people}}{{name}}{{/list}}{{#bool}}true: {{name}}{{/bool}}{{#nobool}}false: {{name}}{{/bool}}"; var ast = Handlebars.parse(string); //require("sys").print(Handlebars.print(ast)); // -var runtime = new Handlebars.Runtime({person: {name: ["Katz", "Yehuda"]}}, {join: function(name, sep) { return name.join(sep); }}) +var runtime = new Handlebars.Runtime({people: [{name: "Yehuda"}, {name: "Leah"}], bool: true, nobool: false, name: "Yehuda Katz"}, + { + list: function(ctx, fn) { + var out = ""; + }, + helperMissing: function(context, fn) { + if(context === true) { + return fn(this); + } else if(context === false) { + return ""; + } + } + } +) runtime.accept(ast) require("sys").print(runtime.buffer)