Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kaanon/gulp-compile-handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaanon MacFarlane committed Sep 11, 2014
2 parents 520fcf2 + 48d31b4 commit ee3917a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
56 changes: 42 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,51 @@ module.exports = function (data, opts) {
}
}

// Do not search for more than 10 nestings
var maxDepth = 10;
// Process only files with given extension names
var allowedExtensions = ['hb', 'hbs', 'handlebars', 'html'];

/**
* Searching partials recursively
* @method mocksearchDirForPartialsPartials
* @param {string} dir directory
* @param {string} registrationDir short directory for registering partial
*/
var searchDirForPartials = function(dir, registrationDir, depth) {
if (depth > maxDepth) {
return;
}

var filenames = fs.readdirSync(dir);

filenames.forEach(function (filename) {
var stats = fs.statSync(dir + '/' + filename);

// If directory, go recursive
if (stats && stats.isDirectory()) {
searchDirForPartials(dir + '/' + filename, registrationDir + '/' + filename, depth + 1);
} else {
// register only files with hb, hbs or handlebars
if (allowedExtensions.indexOf(filename.split('.').pop()) !== -1) {
var name = filename.substr(0, filename.lastIndexOf('.'));

var template = fs.readFileSync(dir + '/' + filename, 'utf8');
Handlebars.registerPartial(registrationDir + '/' + name, template);
// console.log('Registered:', registrationDir + '/' + name)
}
}
});
}


// Go through a partials directory array
if(options.batch){
// Allow single string
if(typeof options.batch === 'string') options.batch = [options.batch];

options.batch.forEach(function (b) {
var filenames = fs.readdirSync(b);

filenames.forEach(function (filename) {
// Needs a better name extractor (maybe with the path module)
var name = filename.split('.')[0];
// Don't allow hidden files
if(!name.length) return;
var template = fs.readFileSync(b + '/' + filename, 'utf8');
Handlebars.registerPartial(b.split('/').pop() + '/' + name, template);
});

options.batch.forEach(function (piece) {
searchDirForPartials(piece, piece.split('/').pop(), 0)
});
}

Expand All @@ -51,7 +79,7 @@ module.exports = function (data, opts) {
partial = match[1];
//Only register an empty partial if the partial has not already been registered
if(!Handlebars.partials.hasOwnProperty(partial)){
Handlebars.registerPartial(partial, gutil.noop);
Handlebars.registerPartial(partial, gutil.noop);
}
}
}
Expand All @@ -72,7 +100,7 @@ module.exports = function (data, opts) {
try {
var fileContents = file.contents.toString();
if(options.ignorePartials){
mockPartials(fileContents);
mockPartials(fileContents);
}
var template = Handlebars.compile(fileContents);
file.contents = new Buffer(template(data));
Expand Down
21 changes: 15 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ npm install --save-dev gulp-compile-handlebars
### `src/hello.handlebars`

```handlebars
<h1>Hello {{firstName}}</h1>
<h2>HELLO! {{capitals firstName}}</h2>
{{> partials/header}}
<p>Hello {{firstName}}</p>
<p>HELLO! {{capitals firstName}}</p>
{{> footer}}
{{> footer2}}
```

### `src/partials/header.handlebars`

```handlebars
<h1>Header</h1>
```

### `gulpfile.js`

```js
var gulp = require('gulp');
var handlebars = require('gulp-compile-handlebars');
var rename = require('gulp-rename');

gulp.task('default', function () {
var templateData = {
Expand All @@ -39,9 +46,10 @@ gulp.task('default', function () {
partials : {
footer : '<footer>the end</footer>'
},
batch : ['./src/partials'],
helpers : {
capitals : function(str){
return str.toUpperCase();
return str.toUpperCase();
}
}
}
Expand All @@ -56,8 +64,9 @@ gulp.task('default', function () {
### `dist/hello.html`

```html
<h1>Hello Kaanon</h1>
<h2>HELLO! KAANON</h2>
<h1>Header</h1>
<p>Hello Kaanon</p>
<p>HELLO! KAANON</p>
<footer>the end</footer>
```

Expand Down

0 comments on commit ee3917a

Please sign in to comment.