Browse files

If a new option, partialsUseNamespace, is set to true then add partia…

…ls to the namespace set in `namespace`.

Closes gh-33.
  • Loading branch information...
1 parent deaf75b commit f7b1733ec2c87ca856047bab939b435271cd218f @pdokas pdokas committed with tkellen Feb 27, 2013
Showing with 64 additions and 3 deletions.
  1. +8 −0 Gruntfile.js
  2. +7 −1 README.md
  3. +6 −0 docs/handlebars-options.md
  4. +6 −2 tasks/handlebars.js
  5. +27 −0 test/expected/partials_use_namespace.js
  6. +10 −0 test/handlebars_test.js
View
8 Gruntfile.js
@@ -152,6 +152,14 @@ module.exports = function(grunt) {
files: {
'tmp/partial_regex.js': ['test/fixtures/par_partial.hbs', 'test/fixtures/one.hbs']
}
+ },
+ partials_use_namespace: {
+ options: {
+ partialsUseNamespace: true
+ },
+ files: {
+ 'tmp/partials_use_namespace.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
+ }
}
},
// Unit tests.
View
8 README.md
@@ -48,6 +48,12 @@ options: {
}
```
+#### partialsUseNamespace
+Type: `Boolean`
+Default: 'false'
+
+When set to `true`, partials will be registered in the `namespace` in addition to templates.
+
#### wrapped
Type: `Boolean`
Default: `false`
@@ -170,4 +176,4 @@ handlebars: {
Task submitted by [Tim Branyen](http://tbranyen.com)
-*This file was generated on Wed Feb 20 2013 12:35:48.*
+*This file was generated on Wed Feb 27 2013 09:30:22.*
View
6 docs/handlebars-options.md
@@ -19,6 +19,12 @@ options: {
}
```
+## partialsUseNamespace
+Type: `Boolean`
+Default: 'false'
+
+When set to `true`, partials will be registered in the `namespace` in addition to templates.
+
## wrapped
Type: `Boolean`
Default: `false`
View
8 tasks/handlebars.js
@@ -35,7 +35,7 @@ module.exports = function(grunt) {
grunt.verbose.writeflags(options, 'Options');
var nsInfo;
- if(options.namespace !== false){
+ if (options.namespace !== false) {
nsInfo = helpers.getNamespaceDeclaration(options.namespace);
}
@@ -83,7 +83,11 @@ module.exports = function(grunt) {
// register partial or add template to namespace
if (isPartial.test(_.last(filepath.split('/')))) {
filename = processPartialName(filepath);
- partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
+ if (options.partialsUseNamespace === true) {
+ partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+');');
+ } else {
+ partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
+ }
} else {
filename = processName(filepath);
if (options.namespace !== false) {
View
27 test/expected/partials_use_namespace.js
@@ -0,0 +1,27 @@
+this["JST"] = this["JST"] || {};
+
+Handlebars.registerPartial("partial", this["JST"]["partial"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ this.compilerInfo = [2,'>= 1.0.0-rc.3'];
+helpers = helpers || Handlebars.helpers; data = data || {};
+
+
+
+ return "<span>Canada</span>";
+ }));
+
+this["JST"]["test/fixtures/one.hbs"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
+ this.compilerInfo = [2,'>= 1.0.0-rc.3'];
+helpers = helpers || Handlebars.helpers; partials = partials || Handlebars.partials; data = data || {};
+ var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression, self=this;
+
+
+ buffer += "<p>Hello, my name is ";
+ if (stack1 = helpers.name) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
+ else { stack1 = depth0.name; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
+ buffer += escapeExpression(stack1)
+ + ". I live in ";
+ stack1 = self.invokePartial(partials.partial, 'partial', depth0, helpers, partials, data);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ buffer += "</p>";
+ return buffer;
+ });
View
10 test/handlebars_test.js
@@ -140,5 +140,15 @@ exports.handlebars = {
test.equal(actual, expected, 'should support custom file name identifiers for partials.');
test.done();
+ },
+ partials_use_namespace: function(test) {
+ 'use strict';
+ test.expect(1);
+
+ var actual = grunt.file.read('tmp/partials_use_namespace.js');
+ var expected = grunt.file.read('test/expected/partials_use_namespace.js');
+ test.equal(actual, expected, 'should allow partials to be added to template namespace.');
+
+ test.done();
}
};

0 comments on commit f7b1733

Please sign in to comment.