Skip to content
Browse files

Allow multiple partial and helper registration

Fixes #369
  • Loading branch information...
1 parent 73f2016 commit 4429ffa9f37620afb211d15480545bd12b54bba5 @kpdecker kpdecker committed Apr 7, 2013
Showing with 104 additions and 15 deletions.
  1. +24 −5 dist/handlebars.js
  2. +24 −5 dist/handlebars.runtime.js
  3. +16 −5 lib/handlebars/base.js
  4. +8 −0 lib/handlebars/utils.js
  5. +32 −0 spec/qunit_spec.js
View
29 dist/handlebars.js
@@ -41,13 +41,26 @@ Handlebars.REVISION_CHANGES = {
Handlebars.helpers = {};
Handlebars.partials = {};
+var toString = Object.prototype.toString,
+ functionType = '[object Function]',
+ objectType = '[object Object]';
+
Handlebars.registerHelper = function(name, fn, inverse) {
- if(inverse) { fn.not = inverse; }
- this.helpers[name] = fn;
+ if (toString.call(name) === objectType) {
+ if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); }
+ Handlebars.Utils.extend(this.helpers, name);
+ } else {
+ if (inverse) { fn.not = inverse; }
+ this.helpers[name] = fn;
+ }
};
Handlebars.registerPartial = function(name, str) {
- this.partials[name] = str;
+ if (toString.call(name) === objectType) {
+ Handlebars.Utils.extend(this.partials, name);
+ } else {
+ this.partials[name] = str;
+ }
};
Handlebars.registerHelper('helperMissing', function(arg) {
@@ -58,8 +71,6 @@ Handlebars.registerHelper('helperMissing', function(arg) {
}
});
-var toString = Object.prototype.toString, functionType = "[object Function]";
-
Handlebars.registerHelper('blockHelperMissing', function(context, options) {
var inverse = options.inverse || function() {}, fn = options.fn;
@@ -818,6 +829,14 @@ var escapeChar = function(chr) {
};
Handlebars.Utils = {
+ extend: function(obj, value) {
+ for(var key in value) {
+ if(value.hasOwnProperty(key)) {
+ obj[key] = value[key];
+ }
+ }
+ },
+
escapeExpression: function(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof Handlebars.SafeString) {
View
29 dist/handlebars.runtime.js
@@ -41,13 +41,26 @@ Handlebars.REVISION_CHANGES = {
Handlebars.helpers = {};
Handlebars.partials = {};
+var toString = Object.prototype.toString,
+ functionType = '[object Function]',
+ objectType = '[object Object]';
+
Handlebars.registerHelper = function(name, fn, inverse) {
- if(inverse) { fn.not = inverse; }
- this.helpers[name] = fn;
+ if (toString.call(name) === objectType) {
+ if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); }
+ Handlebars.Utils.extend(this.helpers, name);
+ } else {
+ if (inverse) { fn.not = inverse; }
+ this.helpers[name] = fn;
+ }
};
Handlebars.registerPartial = function(name, str) {
- this.partials[name] = str;
+ if (toString.call(name) === objectType) {
+ Handlebars.Utils.extend(this.partials, name);
+ } else {
+ this.partials[name] = str;
+ }
};
Handlebars.registerHelper('helperMissing', function(arg) {
@@ -58,8 +71,6 @@ Handlebars.registerHelper('helperMissing', function(arg) {
}
});
-var toString = Object.prototype.toString, functionType = "[object Function]";
-
Handlebars.registerHelper('blockHelperMissing', function(context, options) {
var inverse = options.inverse || function() {}, fn = options.fn;
@@ -204,6 +215,14 @@ var escapeChar = function(chr) {
};
Handlebars.Utils = {
+ extend: function(obj, value) {
+ for(var key in value) {
+ if(value.hasOwnProperty(key)) {
+ obj[key] = value[key];
+ }
+ }
+ },
+
escapeExpression: function(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof Handlebars.SafeString) {
View
21 lib/handlebars/base.js
@@ -18,13 +18,26 @@ Handlebars.REVISION_CHANGES = {
Handlebars.helpers = {};
Handlebars.partials = {};
+var toString = Object.prototype.toString,
+ functionType = '[object Function]',
+ objectType = '[object Object]';
+
Handlebars.registerHelper = function(name, fn, inverse) {
- if(inverse) { fn.not = inverse; }
- this.helpers[name] = fn;
+ if (toString.call(name) === objectType) {
+ if (inverse || fn) { throw new Handlebars.Exception('Arg not supported with multiple helpers'); }
+ Handlebars.Utils.extend(this.helpers, name);
+ } else {
+ if (inverse) { fn.not = inverse; }
+ this.helpers[name] = fn;
+ }
};
Handlebars.registerPartial = function(name, str) {
- this.partials[name] = str;
+ if (toString.call(name) === objectType) {
+ Handlebars.Utils.extend(this.partials, name);
+ } else {
+ this.partials[name] = str;
+ }
};
Handlebars.registerHelper('helperMissing', function(arg) {
@@ -35,8 +48,6 @@ Handlebars.registerHelper('helperMissing', function(arg) {
}
});
-var toString = Object.prototype.toString, functionType = "[object Function]";
-
Handlebars.registerHelper('blockHelperMissing', function(context, options) {
var inverse = options.inverse || function() {}, fn = options.fn;
View
8 lib/handlebars/utils.js
@@ -39,6 +39,14 @@ var escapeChar = function(chr) {
};
Handlebars.Utils = {
+ extend: function(obj, value) {
+ for(var key in value) {
+ if(value.hasOwnProperty(key)) {
+ obj[key] = value[key];
+ }
+ }
+ },
+
escapeExpression: function(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof Handlebars.SafeString) {
View
32 spec/qunit_spec.js
@@ -492,6 +492,27 @@ test("the helpers hash is available is nested contexts", function() {
"helpers hash is available in nested contexts.");
});
+test("Multiple global helper registration", function() {
+ var helpers = Handlebars.helpers;
+ try {
+ Handlebars.helpers = {};
+ Handlebars.registerHelper({
+ 'if': helpers['if'],
+ world: function() { return "world!"; },
+ test_helper: function() { return 'found it!'; }
+ });
+
+ shouldCompileTo(
+ "{{test_helper}} {{#if cruel}}Goodbye {{cruel}} {{world}}!{{/if}}",
+ [{cruel: "cruel"}],
+ "found it! Goodbye cruel world!!");
+ } finally {
+ if (helpers) {
+ Handlebars.helpers = helpers;
+ }
+ }
+});
+
suite("partials");
test("basic partials", function() {
@@ -556,6 +577,17 @@ test("Partials with slash paths", function() {
shouldCompileToWithPartials(string, [hash, {}, {'shared/dude':dude}], true, "Dudes: Jeepers", "Partials can use literal paths");
});
+test("Multiple partial registration", function() {
+ Handlebars.registerPartial({
+ 'shared/dude': '{{name}}',
+ global_test: '{{another_dude}}'
+ });
+
+ var string = "Dudes: {{> shared/dude}} {{> global_test}}";
+ var hash = {name:"Jeepers", another_dude:"Creepers"};
+ shouldCompileToWithPartials(string, [hash], true, "Dudes: Jeepers Creepers", "Partials can use globals or passed");
+});
+
test("Partials with integer path", function() {
var string = "Dudes: {{> 404}}";
var dude = "{{name}}";

0 comments on commit 4429ffa

Please sign in to comment.
Something went wrong with that request. Please try again.