diff --git a/HISTORY.md b/HISTORY.md index 23f2044..8ba8747 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========== + * Add `rename` option to `registerPartials` * Ensure all partials are registered before rendering * Fix function context in async helpers * deps: walk@2.3.15 diff --git a/README.md b/README.md index 6f0667b..09d75ce 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,19 @@ template-file.html -> {{> template_file}} See the [handlebars.js documentation](https://handlebarsjs.com/) for more information. +The way the file is renamed to a partial name can be adjusted by providing a `rename` option. The function will recieve the file path relative to the registered directory and without the file extension. If the returned value contains any whitespace, those characters are replaced with a corresponding underscore character. + +```js +var hbs = require('hbs') + +hbs.registerPartials(path.join(__dirname, '/views/partials'), { + rename: function (name) { + // all non-word characters replaced with underscores + return name.replace(/\W/g, '_') + } +}) +``` + **Note:** This method is async; meaning that the directory is walked in a non-blocking manner to app startup. ## Exposing locals as template data ## diff --git a/lib/hbs.js b/lib/hbs.js index a5f7e9e..21264b6 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -231,7 +231,7 @@ Instance.prototype.registerPartial = function () { this.handlebars.registerPartial.apply(this.handlebars, arguments); }; -Instance.prototype.registerPartials = function (directory, done) { +Instance.prototype.registerPartials = function (directory, options, done) { var self = this if (this._queue) { @@ -242,7 +242,20 @@ Instance.prototype.registerPartials = function (directory, done) { self._queue = [] } + var callback var handlebars = self.handlebars + var opts = options || {} + + if (done || typeof options !== 'function') { + callback = done + } else { + callback = options + opts = {} + } + + var rename = opts.rename !== undefined ? opts.rename : function (name) { + return name.replace(/\-/g, '_') + } var w = walk(directory) w.on('file', function (root, stat, done) { @@ -255,11 +268,12 @@ Instance.prototype.registerPartials = function (directory, done) { fs.readFile(filepath, 'utf8', function(err, data) { if (!err) { - var ext = path.extname(filepath); - var templateName = path.relative(directory, filepath) - .slice(0, -(ext.length)).replace(/[ -]/g, '_') + var extname = path.extname(filepath) + var name = path.relative(directory, filepath) + .slice(0, -(extname.length)) .replace(/\\/g, '/') - handlebars.registerPartial(templateName, data); + + handlebars.registerPartial(rename(name).replace(/ /g, '_'), data) } done(err); @@ -277,8 +291,8 @@ Instance.prototype.registerPartials = function (directory, done) { } }) - if (done) { - w.on('end', done) + if (callback) { + w.on('end', callback) } }; diff --git a/test/4.x/app.js b/test/4.x/app.js index 92f90fa..73abd71 100644 --- a/test/4.x/app.js +++ b/test/4.x/app.js @@ -235,7 +235,7 @@ test('helper error', function (done) { test('partials', function(done) { request(app) .get('/partials') - .expect(shouldHaveFirstLineEqual('Test Partial 1Test Partial 2Test Partial 3Test Partial 4')) + .expect(shouldHaveFirstLineEqual('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5')) .end(done) }); diff --git a/test/4.x/register_partials.js b/test/4.x/register_partials.js index ef5d5bc..c259761 100644 --- a/test/4.x/register_partials.js +++ b/test/4.x/register_partials.js @@ -25,7 +25,7 @@ test('render waits on register partials', function (done) { request(app) .get('/') - .expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4') + .expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5') .end(done) }) @@ -47,7 +47,7 @@ test('render waits on multiple register partials', function (done) { request(app) .get('/') - .expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4') + .expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5') .end(done) }) @@ -56,3 +56,27 @@ test('register partials callback', function (done) { hbs.registerPartials(path.join(__dirname, 'views', 'partials'), done) }) + +test('register partials name', function (done) { + var express = require('express') + var app = express() + var hbs = require('../../').create() + + hbs.registerPartials(path.join(__dirname, 'views', 'partials'), { + rename: function (name) { return name.replace(/(^|\s)(\w)/g, function (s, p, c) { return p + c.toUpperCase() }) } + }) + + app.engine('hbs', hbs.__express) + app.engine('html', hbs.__express) + app.set('view engine', 'hbs') + app.set('views', path.join(__dirname, 'views')) + + app.get('/', function (req, res) { + res.render('partials2', { layout: false }) + }) + + request(app) + .get('/') + .expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5') + .end(done) +}) diff --git a/test/4.x/views/partials.hbs b/test/4.x/views/partials.hbs index b92b909..42561d6 100644 --- a/test/4.x/views/partials.hbs +++ b/test/4.x/views/partials.hbs @@ -2,3 +2,4 @@ {{> partial2}} {{> subdir/partial3}} {{> subdir/subsubdir/partial4}} +{{> part_name_5}} diff --git a/test/4.x/views/partials/part name-5.hbs b/test/4.x/views/partials/part name-5.hbs new file mode 100644 index 0000000..7f0c216 --- /dev/null +++ b/test/4.x/views/partials/part name-5.hbs @@ -0,0 +1 @@ +Test Partial 5 \ No newline at end of file diff --git a/test/4.x/views/partials2.hbs b/test/4.x/views/partials2.hbs new file mode 100644 index 0000000..c0d2fa4 --- /dev/null +++ b/test/4.x/views/partials2.hbs @@ -0,0 +1,5 @@ +{{> Partial1}} +{{> Partial2}} +{{> Subdir/partial3}} +{{> Subdir/subsubdir/partial4}} +{{> Part_Name-5}}