Skip to content

Commit

Permalink
data should be passed through to partials. closes handlebars-lang#111.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehuda Katz authored and Yehuda Katz committed Dec 27, 2011
1 parent 69307d0 commit e474e56
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,13 @@ Handlebars.JavaScriptCompiler = function() {};
},

invokePartial: function(context) {
this.pushStack("self.invokePartial(" + this.nameLookup('partials', context, 'partial') + ", '" + context + "', " + this.popStack() + ", helpers, partials);");
params = [this.nameLookup('partials', context, 'partial'), "'" + context + "'", this.popStack(), "helpers", "partials"];

if (this.options.data) {
params.push("data");
}

this.pushStack("self.invokePartial(" + params.join(", ") + ");");
},

assignToHash: function(key) {
Expand Down
8 changes: 5 additions & 3 deletions lib/handlebars/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ Handlebars.VM = {
};
},
noop: function() { return ""; },
invokePartial: function(partial, name, context, helpers, partials) {
invokePartial: function(partial, name, context, helpers, partials, data) {
options = { helpers: helpers, partials: partials, data: data };

if(partial === undefined) {
throw new Handlebars.Exception("The partial " + name + " could not be found");
} else if(partial instanceof Function) {
return partial(context, {helpers: helpers, partials: partials});
return partial(context, options);
} else if (!Handlebars.compile) {
throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in vm mode");
} else {
partials[name] = Handlebars.compile(partial);
return partials[name](context, {helpers: helpers, partials: partials});
return partials[name](context, options);
}
}
};
Expand Down
17 changes: 17 additions & 0 deletions spec/qunit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,23 @@ test("passing in data to a compiled function that expects data - works with help
equals("happy cat", result, "Data output by helper");
});

test("passing in data to a compiled function that expects data - works with helpers in partials", function() {
var template = CompilerContext.compile("{{>my_partial}}", {data: true});

var partials = {
my_partial: CompilerContext.compile("{{hello}}", {data: true})
};

var helpers = {
hello: function(options) {
return options.data.adjective + " " + this.noun;
}
};

var result = template({noun: "cat"}, {helpers: helpers, partials: partials, data: {adjective: "happy"}});
equals("happy cat", result, "Data output by helper inside partial");
});

test("passing in data to a compiled function that expects data - works with helpers and parameters", function() {
var template = CompilerContext.compile("{{hello world}}", {data: true});

Expand Down

0 comments on commit e474e56

Please sign in to comment.