Skip to content

Commit 73559c1

Browse files
committed
Add rename option to registerPartials
closes #164
1 parent 3b34543 commit 73559c1

File tree

8 files changed

+69
-10
lines changed

8 files changed

+69
-10
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Add `rename` option to `registerPartials`
45
* Ensure all partials are registered before rendering
56
* Fix function context in async helpers
67

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ template-file.html -> {{> template_file}}
6161
See the [handlebars.js documentation](https://handlebarsjs.com/) for more
6262
information.
6363

64+
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.
65+
66+
```js
67+
var hbs = require('hbs')
68+
69+
hbs.registerPartials(path.join(__dirname, '/views/partials'), {
70+
rename: function (name) {
71+
// all non-word characters replaced with underscores
72+
return name.replace(/\W/g, '_')
73+
}
74+
})
75+
```
76+
6477
**Note:** This method is async; meaning that the directory is walked in a non-blocking manner to app startup.
6578

6679
## Exposing locals as template data ##

lib/hbs.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Instance.prototype.registerPartial = function () {
231231
this.handlebars.registerPartial.apply(this.handlebars, arguments);
232232
};
233233

234-
Instance.prototype.registerPartials = function (directory, done) {
234+
Instance.prototype.registerPartials = function (directory, options, done) {
235235
var self = this
236236

237237
if (this._queue) {
@@ -242,7 +242,20 @@ Instance.prototype.registerPartials = function (directory, done) {
242242
self._queue = []
243243
}
244244

245+
var callback
245246
var handlebars = self.handlebars
247+
var opts = options || {}
248+
249+
if (done || typeof options !== 'function') {
250+
callback = done
251+
} else {
252+
callback = options
253+
opts = {}
254+
}
255+
256+
var rename = opts.rename !== undefined ? opts.rename : function (name) {
257+
return name.replace(/\-/g, '_')
258+
}
246259

247260
var w = walk(directory)
248261
w.on('file', function (root, stat, done) {
@@ -255,11 +268,12 @@ Instance.prototype.registerPartials = function (directory, done) {
255268

256269
fs.readFile(filepath, 'utf8', function(err, data) {
257270
if (!err) {
258-
var ext = path.extname(filepath);
259-
var templateName = path.relative(directory, filepath)
260-
.slice(0, -(ext.length)).replace(/[ -]/g, '_')
271+
var extname = path.extname(filepath)
272+
var name = path.relative(directory, filepath)
273+
.slice(0, -(extname.length))
261274
.replace(/\\/g, '/')
262-
handlebars.registerPartial(templateName, data);
275+
276+
handlebars.registerPartial(rename(name).replace(/ /g, '_'), data)
263277
}
264278

265279
done(err);
@@ -277,8 +291,8 @@ Instance.prototype.registerPartials = function (directory, done) {
277291
}
278292
})
279293

280-
if (done) {
281-
w.on('end', done)
294+
if (callback) {
295+
w.on('end', callback)
282296
}
283297
};
284298

test/4.x/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ test('helper error', function (done) {
235235
test('partials', function(done) {
236236
request(app)
237237
.get('/partials')
238-
.expect(shouldHaveFirstLineEqual('Test Partial 1Test Partial 2Test Partial 3Test Partial 4'))
238+
.expect(shouldHaveFirstLineEqual('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5'))
239239
.end(done)
240240
});
241241

test/4.x/register_partials.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('render waits on register partials', function (done) {
2525

2626
request(app)
2727
.get('/')
28-
.expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4')
28+
.expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5')
2929
.end(done)
3030
})
3131

@@ -47,7 +47,7 @@ test('render waits on multiple register partials', function (done) {
4747

4848
request(app)
4949
.get('/')
50-
.expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4')
50+
.expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5')
5151
.end(done)
5252
})
5353

@@ -56,3 +56,27 @@ test('register partials callback', function (done) {
5656

5757
hbs.registerPartials(path.join(__dirname, 'views', 'partials'), done)
5858
})
59+
60+
test('register partials name', function (done) {
61+
var express = require('express')
62+
var app = express()
63+
var hbs = require('../../').create()
64+
65+
hbs.registerPartials(path.join(__dirname, 'views', 'partials'), {
66+
rename: function (name) { return name.replace(/(^|\s)(\w)/g, function (s, p, c) { return p + c.toUpperCase() }) }
67+
})
68+
69+
app.engine('hbs', hbs.__express)
70+
app.engine('html', hbs.__express)
71+
app.set('view engine', 'hbs')
72+
app.set('views', path.join(__dirname, 'views'))
73+
74+
app.get('/', function (req, res) {
75+
res.render('partials2', { layout: false })
76+
})
77+
78+
request(app)
79+
.get('/')
80+
.expect('Test Partial 1Test Partial 2Test Partial 3Test Partial 4Test Partial 5')
81+
.end(done)
82+
})

test/4.x/views/partials.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
{{> partial2}}
33
{{> subdir/partial3}}
44
{{> subdir/subsubdir/partial4}}
5+
{{> part_name_5}}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test Partial 5

test/4.x/views/partials2.hbs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{> Partial1}}
2+
{{> Partial2}}
3+
{{> Subdir/partial3}}
4+
{{> Subdir/subsubdir/partial4}}
5+
{{> Part_Name-5}}

0 commit comments

Comments
 (0)