Skip to content

Commit

Permalink
Merge pull request #182 from wycats/partial-hash
Browse files Browse the repository at this point in the history
Context different for partial
  • Loading branch information
kpdecker committed Jan 18, 2014
2 parents 93a3725 + e290ec2 commit 363cb4b
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 9 deletions.
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) {

This comment has been minimized.

Copy link
@machty

machty Jan 18, 2014

Contributor

This breaks Emblem. Could we move hash to the end?

This comment has been minimized.

Copy link
@kpdecker

kpdecker Jan 18, 2014

Author Collaborator

There are many breaking changes coming in 2.0 across the board. I put this here to keep the call signatures logically similar rather than just bolting things on the end.

This comment has been minimized.

Copy link
@machty

machty Jan 18, 2014

Contributor

Ah ok, so the next release is 2.0? I can let whoever reported this know they shouldn't use anything beyond the 1.3 release

This comment has been minimized.

Copy link
@kpdecker

kpdecker via email Jan 18, 2014

Author Collaborator
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

0 comments on commit 363cb4b

Please sign in to comment.