Skip to content

Commit

Permalink
add registerPartials function for loading all partials in a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrue committed May 30, 2013
1 parent 544f5b0 commit 11f35d1
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ hbs.registerHelper('helper_name', function(...) { ... });
hbs.registerPartial('partial_name', 'partial value');
```

For convenience, `registerPartials` provides a quick way to load all partials from a specific directory:

```javascript
var hbs = require('hbs');

hbs.registerPartials(__dirname + '/views/partials');
```

Partials that are loaded from a directory are named based on their filename, where spaces and hyphens are replaced with an underscore character:

```
template.html -> {{> template}}
template 2.html -> {{> template_2}}
login view.hbs -> {{> login_view}}
template-file.html -> {{> template_file}}
```

See the [handlebars.js](http://github.com/wycats/handlebars.js) README and docs for more information.

## handlebars ##
Expand Down
1 change: 1 addition & 0 deletions examples/partial/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var hbs = require('hbs');
var app = express();

hbs.registerPartial('partial', fs.readFileSync(__dirname + '/views/partial.hbs', 'utf8'));
hbs.registerPartials(__dirname + '/views/partials');

// set the view engine to use handlebars
app.set('view engine', 'hbs');
Expand Down
5 changes: 5 additions & 0 deletions examples/partial/views/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ regular body

{{> partial}}

{{> partial1}}
{{> partial2}}
{{> partial3}}
{{> partial4}}
{{> partial5}}
1 change: 1 addition & 0 deletions examples/partial/views/layout.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
post body
<hr/>

{{> partial5 }}
</body>

</html>
1 change: 1 addition & 0 deletions examples/partial/views/partials/partial1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Partial 1 Content</div>
1 change: 1 addition & 0 deletions examples/partial/views/partials/partial2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Partial 2 Content</div>
1 change: 1 addition & 0 deletions examples/partial/views/partials/partial3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Partial 3 Content</div>
1 change: 1 addition & 0 deletions examples/partial/views/partials/partial4.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Partial 4 Content</div>
1 change: 1 addition & 0 deletions examples/partial/views/partials/partial5.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Partial 5 Content</div>
37 changes: 37 additions & 0 deletions lib/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
var fs = require('fs');
var path = require('path');

// handle simple flow control
var after = require('after');

// handle async helpers
var async = require('./async');

Expand Down Expand Up @@ -159,6 +162,40 @@ Instance.prototype.registerPartial = function () {
this.handlebars.registerPartial.apply(this.handlebars, arguments);
};

Instance.prototype.registerPartials = function (directory, done) {
var handlebars = this.handlebars;

var register = function(filepath, done) {
var isValidTemplate = /\.(html|hbs)$/.test(filepath);

if (!isValidTemplate) {
return done(null);
}

fs.readFile(filepath, 'utf8', function(err, data) {
if (!err) {
var templateName = path.basename(filepath, path.extname(filepath));
templateName = templateName.replace(/[ -]/g, '_');
handlebars.registerPartial(templateName, data);
}

done(err);
});
};

fs.readdir(directory, function(err, filenames) {
if (err) {
return done(err);
}

var next = after(filenames.length, done || function() {});

filenames.forEach(function(filename) {
register(path.join(directory, filename), next);
});
});
};

Instance.prototype.registerAsyncHelper = function(name, fn) {
this.handlebars.registerHelper(name, function(context) {
return async.resolve(fn, context);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "hbs",
"description": "Express.js template engine plugin for Handlebars",
"version": "2.1.0",
"version": "2.2.0",
"homepage": "https://github.com/donpark/hbs",
"author": "Don Park <[email protected]> (http://blog.docuverse.com)",
"main": "lib/hbs.js",
"directories": {
"lib": "./lib"
},
"dependencies": {
"handlebars": "1.0.9"
"handlebars": "1.0.9",
"after": "0.7.0"
},
"devDependencies": {
"mocha": "1.6.0",
Expand Down
21 changes: 21 additions & 0 deletions test/3.x/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ hbs.registerHelper('list', function(items, context) {
});

hbs.registerPartial('link2', '<a href="/people/{{id}}">{{name}}</a>');
hbs.registerPartials(__dirname + '/views/partials');

app.get('/', function(req, res){
res.render('index', {
Expand Down Expand Up @@ -115,6 +116,10 @@ app.get('/syntax-error', function(req, res) {
res.render('syntax-error');
});

app.get('/partials', function(req, res) {
res.render('partials', { layout: false });
});

app.get('/escape', function(req, res) {
res.render('escape', { title: 'foobar', layout: false });
});
Expand All @@ -135,6 +140,22 @@ test('index', function(done) {
});
});

test('partials', function(done) {
var server = app.listen(3000, function() {

var expected = 'Test Partial 1\nTest Partial 2';

request('http://localhost:3000/partials', function(err, res, body) {
assert.equal(body, expected);
server.close();
});
});

server.on('close', function() {
done();
});
});

test('html extension', function(done) {
var server = app.listen(3000, function() {

Expand Down
2 changes: 2 additions & 0 deletions test/3.x/views/partials.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{> partial1}}
{{> partial2}}
Binary file added test/3.x/views/partials/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions test/3.x/views/partials/partial1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test Partial 1
1 change: 1 addition & 0 deletions test/3.x/views/partials/partial2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test Partial 2

0 comments on commit 11f35d1

Please sign in to comment.