summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/handlebars/compiler/ast.js3
-rw-r--r--lib/handlebars/compiler/compiler.js10
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js2
-rw-r--r--lib/handlebars/compiler/printer.js7
-rw-r--r--lib/handlebars/runtime.js8
-rw-r--r--spec/ast.js2
-rw-r--r--spec/parser.js8
-rw-r--r--spec/partials.js8
-rw-r--r--src/handlebars.yy3
9 files changed, 42 insertions, 9 deletions
diff --git a/lib/handlebars/compiler/ast.js b/lib/handlebars/compiler/ast.js
index dda682e..5538f40 100644
--- a/lib/handlebars/compiler/ast.js
+++ b/lib/handlebars/compiler/ast.js
@@ -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;
},
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index 3c4a9b7..b92289a 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -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');
}
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index 7898bd1..8ca0116 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -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");
diff --git a/lib/handlebars/compiler/printer.js b/lib/handlebars/compiler/printer.js
index ad55c7d..b7b760f 100644
--- a/lib/handlebars/compiler/printer.js
+++ b/lib/handlebars/compiler/printer.js
@@ -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 + " }}");
};
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js
index 699499c..5df681f 100644
--- a/lib/handlebars/runtime.js
+++ b/lib/handlebars/runtime.js
@@ -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) {
diff --git a/spec/ast.js b/spec/ast.js
index 46f0131..430574e 100644
--- a/spec/ast.js
+++ b/spec/ast.js
@@ -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);
});
});
diff --git a/spec/parser.js b/spec/parser.js
index 70c1635..0f6ed31 100644
--- a/spec/parser.js
+++ b/spec/parser.js
@@ -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");
});
diff --git a/spec/partials.js b/spec/partials.js
index bea72f5..732436a 100644
--- a/spec/partials.js
+++ b/spec/partials.js
@@ -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}} ";
diff --git a/src/handlebars.yy b/src/handlebars.yy
index 7bff512..bac1cc9 100644
--- a/src/handlebars.yy
+++ b/src/handlebars.yy
@@ -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