Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context different for partial #182

Merged
merged 3 commits into from
Jan 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/handlebars/compiler/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ var AST = {
// pass or at runtime.
},

PartialNode: function(partialName, context, strip, locInfo) {
PartialNode: function(partialName, context, hash, strip, locInfo) {
LocationInfo.call(this, locInfo);
this.type = "partial";
this.partialName = partialName;
this.context = context;
this.hash = hash;
this.strip = strip;
},

Expand Down
10 changes: 8 additions & 2 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,14 @@ Compiler.prototype = {
var partialName = partial.partialName;
this.usePartial = true;

if(partial.context) {
this.ID(partial.context);
if (partial.hash) {
this.accept(partial.hash);
} else {
this.opcode('push', 'undefined');
}

if (partial.context) {
this.accept(partial.context);
} else {
this.opcode('push', 'depth0');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ JavaScriptCompiler.prototype = {
// This operation pops off a context, invokes a partial with that context,
// and pushes the result of the invocation back.
invokePartial: function(name) {
var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), this.popStack(), "helpers", "partials"];

if (this.options.data) {
params.push("data");
Expand Down
7 changes: 6 additions & 1 deletion lib/handlebars/compiler/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ PrintVisitor.prototype.mustache = function(mustache) {

PrintVisitor.prototype.partial = function(partial) {
var content = this.accept(partial.partialName);
if(partial.context) { content = content + " " + this.accept(partial.context); }
if(partial.context) {
content += " " + this.accept(partial.context);
}
if (partial.hash) {
content += " " + this.accept(partial.hash);
}
return this.pad("{{> " + content + " }}");
};

Expand Down
8 changes: 6 additions & 2 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ export function template(templateSpec, env) {

// Note: Using env.VM references rather than local var references throughout this section to allow
// for external users to override these as psuedo-supported APIs.
var invokePartialWrapper = function(partial, name, context, helpers, partials, data) {
var result = env.VM.invokePartial.apply(this, arguments);
var invokePartialWrapper = function(partial, name, context, hash, helpers, partials, data) {
if (hash) {
context = Utils.extend({}, context, hash);
}

var result = env.VM.invokePartial.call(this, partial, name, context, helpers, partials, data);
if (result != null) { return result; }

if (env.compile) {
Expand Down
2 changes: 1 addition & 1 deletion spec/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('ast', function() {
describe("PartialNode", function(){

it('stores location info', function(){
var pn = new handlebarsEnv.AST.PartialNode("so_partial", {}, {}, LOCATION_INFO);
var pn = new handlebarsEnv.AST.PartialNode("so_partial", {}, {}, {}, LOCATION_INFO);
testLocationInfoStorage(pn);
});
});
Expand Down
8 changes: 8 additions & 0 deletions spec/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ describe('parser', function() {
equals(ast_for("{{> foo bar}}"), "{{> PARTIAL:foo ID:bar }}\n");
});

it('parses a partial with hash', function() {
equals(ast_for("{{> foo bar=bat}}"), "{{> PARTIAL:foo HASH{bar=ID:bat} }}\n");
});

it('parses a partial with context and hash', function() {
equals(ast_for("{{> foo bar bat=baz}}"), "{{> PARTIAL:foo ID:bar HASH{bat=ID:baz} }}\n");
});

it('parses a partial with a complex name', function() {
equals(ast_for("{{> shared/partial?.bar}}"), "{{> PARTIAL:shared/partial?.bar }}\n");
});
Expand Down
8 changes: 8 additions & 0 deletions spec/partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ describe('partials', function() {
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: Empty");
});

it("partials with parameters", function() {
var string = "Dudes: {{#dudes}}{{> dude others=..}}{{/dudes}}";
var partial = "{{others.foo}}{{name}} ({{url}}) ";
var hash = {foo: 'bar', dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]};
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: barYehuda (http://yehuda) barAlan (http://alan) ",
"Basic partials output based on current context.");
});

it("partial in a partial", function() {
var string = "Dudes: {{#dudes}}{{>dude}}{{/dudes}}";
var dude = "{{name}} {{> url}} ";
Expand Down
3 changes: 2 additions & 1 deletion src/handlebars.yy
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ mustache
;

partial
: OPEN_PARTIAL partialName path? CLOSE -> new yy.PartialNode($2, $3, stripFlags($1, $4), @$)
: OPEN_PARTIAL partialName param hash? CLOSE -> new yy.PartialNode($2, $3, $4, stripFlags($1, $5), @$)
| OPEN_PARTIAL partialName hash? CLOSE -> new yy.PartialNode($2, undefined, $3, stripFlags($1, $4), @$)
;

simpleInverse
Expand Down