Skip to content

Commit

Permalink
Allow multiple partial and helper registration
Browse files Browse the repository at this point in the history
Fixes #369
  • Loading branch information
kpdecker committed Apr 7, 2013
1 parent 73f2016 commit 4429ffa
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 15 deletions.
29 changes: 24 additions & 5 deletions dist/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 24 additions & 5 deletions dist/handlebars.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 16 additions & 5 deletions lib/handlebars/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions lib/handlebars/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions spec/qunit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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}}";
Expand Down

0 comments on commit 4429ffa

Please sign in to comment.