Skip to content

Commit 363cb4b

Browse files
committed
Merge pull request #182 from wycats/partial-hash
Context different for partial
2 parents 93a3725 + e290ec2 commit 363cb4b

File tree

9 files changed

+42
-9
lines changed

9 files changed

+42
-9
lines changed

lib/handlebars/compiler/ast.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ var AST = {
9797
// pass or at runtime.
9898
},
9999

100-
PartialNode: function(partialName, context, strip, locInfo) {
100+
PartialNode: function(partialName, context, hash, strip, locInfo) {
101101
LocationInfo.call(this, locInfo);
102102
this.type = "partial";
103103
this.partialName = partialName;
104104
this.context = context;
105+
this.hash = hash;
105106
this.strip = strip;
106107
},
107108

lib/handlebars/compiler/compiler.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,14 @@ Compiler.prototype = {
203203
var partialName = partial.partialName;
204204
this.usePartial = true;
205205

206-
if(partial.context) {
207-
this.ID(partial.context);
206+
if (partial.hash) {
207+
this.accept(partial.hash);
208+
} else {
209+
this.opcode('push', 'undefined');
210+
}
211+
212+
if (partial.context) {
213+
this.accept(partial.context);
208214
} else {
209215
this.opcode('push', 'depth0');
210216
}

lib/handlebars/compiler/javascript-compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ JavaScriptCompiler.prototype = {
569569
// This operation pops off a context, invokes a partial with that context,
570570
// and pushes the result of the invocation back.
571571
invokePartial: function(name) {
572-
var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), "helpers", "partials"];
572+
var params = [this.nameLookup('partials', name, 'partial'), "'" + name + "'", this.popStack(), this.popStack(), "helpers", "partials"];
573573

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

lib/handlebars/compiler/printer.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ PrintVisitor.prototype.mustache = function(mustache) {
8282

8383
PrintVisitor.prototype.partial = function(partial) {
8484
var content = this.accept(partial.partialName);
85-
if(partial.context) { content = content + " " + this.accept(partial.context); }
85+
if(partial.context) {
86+
content += " " + this.accept(partial.context);
87+
}
88+
if (partial.hash) {
89+
content += " " + this.accept(partial.hash);
90+
}
8691
return this.pad("{{> " + content + " }}");
8792
};
8893

lib/handlebars/runtime.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ export function template(templateSpec, env) {
2929

3030
// Note: Using env.VM references rather than local var references throughout this section to allow
3131
// for external users to override these as psuedo-supported APIs.
32-
var invokePartialWrapper = function(partial, name, context, helpers, partials, data) {
33-
var result = env.VM.invokePartial.apply(this, arguments);
32+
var invokePartialWrapper = function(partial, name, context, hash, helpers, partials, data) {
33+
if (hash) {
34+
context = Utils.extend({}, context, hash);
35+
}
36+
37+
var result = env.VM.invokePartial.call(this, partial, name, context, helpers, partials, data);
3438
if (result != null) { return result; }
3539

3640
if (env.compile) {

spec/ast.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('ast', function() {
193193
describe("PartialNode", function(){
194194

195195
it('stores location info', function(){
196-
var pn = new handlebarsEnv.AST.PartialNode("so_partial", {}, {}, LOCATION_INFO);
196+
var pn = new handlebarsEnv.AST.PartialNode("so_partial", {}, {}, {}, LOCATION_INFO);
197197
testLocationInfoStorage(pn);
198198
});
199199
});

spec/parser.js

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ describe('parser', function() {
8484
equals(ast_for("{{> foo bar}}"), "{{> PARTIAL:foo ID:bar }}\n");
8585
});
8686

87+
it('parses a partial with hash', function() {
88+
equals(ast_for("{{> foo bar=bat}}"), "{{> PARTIAL:foo HASH{bar=ID:bat} }}\n");
89+
});
90+
91+
it('parses a partial with context and hash', function() {
92+
equals(ast_for("{{> foo bar bat=baz}}"), "{{> PARTIAL:foo ID:bar HASH{bat=ID:baz} }}\n");
93+
});
94+
8795
it('parses a partial with a complex name', function() {
8896
equals(ast_for("{{> shared/partial?.bar}}"), "{{> PARTIAL:shared/partial?.bar }}\n");
8997
});

spec/partials.js

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ describe('partials', function() {
2323
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: Empty");
2424
});
2525

26+
it("partials with parameters", function() {
27+
var string = "Dudes: {{#dudes}}{{> dude others=..}}{{/dudes}}";
28+
var partial = "{{others.foo}}{{name}} ({{url}}) ";
29+
var hash = {foo: 'bar', dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]};
30+
shouldCompileToWithPartials(string, [hash, {}, {dude: partial}], true, "Dudes: barYehuda (http://yehuda) barAlan (http://alan) ",
31+
"Basic partials output based on current context.");
32+
});
33+
2634
it("partial in a partial", function() {
2735
var string = "Dudes: {{#dudes}}{{>dude}}{{/dudes}}";
2836
var dude = "{{name}} {{> url}} ";

src/handlebars.yy

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ mustache
6363
;
6464

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

6970
simpleInverse

0 commit comments

Comments
 (0)