Permalink
Browse files

Adds a partialsPathRegex option

This option is useful for large codebases that organize partials into a subdirectory of their templates. When used in combination with a lax partialRegex filter, partials inside the specified subdirectory can leave off the leading _ in their filename.

Closes gh-32
  • Loading branch information...
1 parent 1af3618 commit fd2d61787ad2ad9caa76472567cf23749812881e @pdokas pdokas committed with tkellen Feb 22, 2013
Showing with 116 additions and 40 deletions.
  1. +37 −28 Gruntfile.js
  2. +14 −1 README.md
  3. +13 −0 docs/handlebars-options.md
  4. +4 −1 tasks/handlebars.js
  5. +27 −0 test/expected/partials_path_regex.js
  6. +1 −0 test/fixtures/partials/partial.hbs
  7. +20 −10 test/handlebars_test.js
View
@@ -84,26 +84,26 @@ module.exports = function(grunt) {
files: {
'tmp/no_namespace.js': ['test/fixtures/basic.hbs']
}
- },
- processcontent: {
- options: {
- processContent: function(content) {
- content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
- content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]+$/, '');
- return content;
- }
- },
- files: {
- 'tmp/processcontent.js': ['test/fixtures/has-spaces.hbs']
- }
- },
- amd_compile: {
- options: {
- amd: true
- },
- files: {
- 'tmp/amd_compile.js': ['test/fixtures/amd.html']
- }
+ },
+ processcontent: {
+ options: {
+ processContent: function(content) {
+ content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
+ content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]+$/, '');
+ return content;
+ }
+ },
+ files: {
+ 'tmp/processcontent.js': ['test/fixtures/has-spaces.hbs']
+ }
+ },
+ amd_compile: {
+ options: {
+ amd: true
+ },
+ files: {
+ 'tmp/amd_compile.js': ['test/fixtures/amd.html']
+ }
},
amd_compile_direct: {
options: {
@@ -152,14 +152,23 @@ 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']
- }
+ },
+ partials_use_namespace: {
+ options: {
+ partialsUseNamespace: true
+ },
+ files: {
+ 'tmp/partials_use_namespace.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
+ }
+ },
+ partials_path_regex: {
+ options: {
+ partialRegex: /.*/,
+ partialsPathRegex: /\/partials\//
+ },
+ files: {
+ 'tmp/partials_path_regex.js': ['test/fixtures/partials/partial.hbs', 'test/fixtures/one.hbs']
+ }
}
},
// Unit tests.
View
@@ -138,6 +138,19 @@ options: {
}
```
+#### partialsPathRegex
+Type: `Regexp`
+Default: '/./'
+
+This option accepts a regex that defines the path to a directory of Handlebars partials files. The example below shows how to mark every file in a specific directory as a partial.
+
+``` javascript
+options: {
+ partialRegex: /.*/,
+ partialsPathRegex: /\/partials\//
+}
+```
+
### Usage Examples
```js
@@ -176,4 +189,4 @@ handlebars: {
Task submitted by [Tim Branyen](http://tbranyen.com)
-*This file was generated on Wed Feb 27 2013 09:30:22.*
+*This file was generated on Wed Feb 27 2013 09:32:54.*
View
@@ -108,3 +108,16 @@ options: {
partialRegex: /^par_/
}
```
+
+## partialsPathRegex
+Type: `Regexp`
+Default: '/./'
+
+This option accepts a regex that defines the path to a directory of Handlebars partials files. The example below shows how to mark every file in a specific directory as a partial.
+
+``` javascript
+options: {
+ partialRegex: /.*/,
+ partialsPathRegex: /\/partials\//
+}
+```
View
@@ -41,6 +41,9 @@ module.exports = function(grunt) {
if (options.namespace !== false) {
nsInfo = helpers.getNamespaceDeclaration(options.namespace);
}
+
+ // assign regex for partials directory detection
+ var partialsPathRegex = options.partialsPathRegex || /./;
// assign regex for partial detection
var isPartial = options.partialRegex || /^_/;
@@ -84,7 +87,7 @@ module.exports = function(grunt) {
}
// register partial or add template to namespace
- if (isPartial.test(_.last(filepath.split('/')))) {
+ if (partialsPathRegex.test(filepath) && isPartial.test(_.last(filepath.split('/')))) {
filename = processPartialName(filepath);
if (options.partialsUseNamespace === true) {
partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+');');
@@ -0,0 +1,27 @@
+this["JST"] = this["JST"] || {};
+
+Handlebars.registerPartial("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;
+ });
@@ -0,0 +1 @@
+<span>Canada</span>
View
@@ -140,15 +140,25 @@ 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();
+ },
+ 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();
+ },
+ partials_path: function(test) {
+ 'use strict';
+ test.expect(1);
+
+ var actual = grunt.file.read('tmp/partials_path_regex.js');
+ var expected = grunt.file.read('test/expected/partials_path_regex.js');
+ test.equal(actual, expected, 'should support custom path to partials.');
+
+ test.done();
}
};

0 comments on commit fd2d617

Please sign in to comment.