Skip to content

Commit

Permalink
Add rename option to registerPartials
Browse files Browse the repository at this point in the history
closes #164
  • Loading branch information
dougwilson committed Oct 1, 2021
1 parent 3b34543 commit 73559c1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Add `rename` option to `registerPartials`
* Ensure all partials are registered before rendering
* Fix function context in async helpers
* deps: [email protected]
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down
28 changes: 21 additions & 7 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -277,8 +291,8 @@ Instance.prototype.registerPartials = function (directory, done) {
}
})

if (done) {
w.on('end', done)
if (callback) {
w.on('end', callback)
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/4.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});

Expand Down
28 changes: 26 additions & 2 deletions test/4.x/register_partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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)
})

Expand All @@ -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)
})
1 change: 1 addition & 0 deletions test/4.x/views/partials.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
{{> partial2}}
{{> subdir/partial3}}
{{> subdir/subsubdir/partial4}}
{{> part_name_5}}
1 change: 1 addition & 0 deletions test/4.x/views/partials/part name-5.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test Partial 5
5 changes: 5 additions & 0 deletions test/4.x/views/partials2.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{> Partial1}}
{{> Partial2}}
{{> Subdir/partial3}}
{{> Subdir/subsubdir/partial4}}
{{> Part_Name-5}}

0 comments on commit 73559c1

Please sign in to comment.