diff --git a/Readme.md b/Readme.md index 092c5da..a2fd15b 100644 --- a/Readme.md +++ b/Readme.md @@ -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 ## diff --git a/examples/partial/app.js b/examples/partial/app.js index fcfde76..b6b606d 100644 --- a/examples/partial/app.js +++ b/examples/partial/app.js @@ -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'); diff --git a/examples/partial/views/index.hbs b/examples/partial/views/index.hbs index 158577c..915cb92 100644 --- a/examples/partial/views/index.hbs +++ b/examples/partial/views/index.hbs @@ -5,3 +5,8 @@ regular body {{> partial}} +{{> partial1}} +{{> partial2}} +{{> partial3}} +{{> partial4}} +{{> partial5}} diff --git a/examples/partial/views/layout.hbs b/examples/partial/views/layout.hbs index 36db91e..73bbc7e 100644 --- a/examples/partial/views/layout.hbs +++ b/examples/partial/views/layout.hbs @@ -12,6 +12,7 @@ post body
+ {{> partial5 }} diff --git a/examples/partial/views/partials/partial1.html b/examples/partial/views/partials/partial1.html new file mode 100644 index 0000000..2beae59 --- /dev/null +++ b/examples/partial/views/partials/partial1.html @@ -0,0 +1 @@ +
Partial 1 Content
diff --git a/examples/partial/views/partials/partial2.html b/examples/partial/views/partials/partial2.html new file mode 100644 index 0000000..54bf44e --- /dev/null +++ b/examples/partial/views/partials/partial2.html @@ -0,0 +1 @@ +
Partial 2 Content
diff --git a/examples/partial/views/partials/partial3.html b/examples/partial/views/partials/partial3.html new file mode 100644 index 0000000..1a19485 --- /dev/null +++ b/examples/partial/views/partials/partial3.html @@ -0,0 +1 @@ +
Partial 3 Content
diff --git a/examples/partial/views/partials/partial4.hbs b/examples/partial/views/partials/partial4.hbs new file mode 100644 index 0000000..4e8ece6 --- /dev/null +++ b/examples/partial/views/partials/partial4.hbs @@ -0,0 +1 @@ +
Partial 4 Content
diff --git a/examples/partial/views/partials/partial5.hbs b/examples/partial/views/partials/partial5.hbs new file mode 100644 index 0000000..85aeecb --- /dev/null +++ b/examples/partial/views/partials/partial5.hbs @@ -0,0 +1 @@ +
Partial 5 Content
diff --git a/lib/hbs.js b/lib/hbs.js index 9678c65..9208530 100644 --- a/lib/hbs.js +++ b/lib/hbs.js @@ -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'); @@ -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); diff --git a/package.json b/package.json index 9447eef..ea95c98 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "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 (http://blog.docuverse.com)", "main": "lib/hbs.js", @@ -9,7 +9,8 @@ "lib": "./lib" }, "dependencies": { - "handlebars": "1.0.9" + "handlebars": "1.0.9", + "after": "0.7.0" }, "devDependencies": { "mocha": "1.6.0", diff --git a/test/3.x/app.js b/test/3.x/app.js index 2465731..8c8b905 100644 --- a/test/3.x/app.js +++ b/test/3.x/app.js @@ -42,6 +42,7 @@ hbs.registerHelper('list', function(items, context) { }); hbs.registerPartial('link2', '{{name}}'); +hbs.registerPartials(__dirname + '/views/partials'); app.get('/', function(req, res){ res.render('index', { @@ -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 }); }); @@ -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() { diff --git a/test/3.x/views/partials.hbs b/test/3.x/views/partials.hbs new file mode 100644 index 0000000..681f690 --- /dev/null +++ b/test/3.x/views/partials.hbs @@ -0,0 +1,2 @@ +{{> partial1}} +{{> partial2}} \ No newline at end of file diff --git a/test/3.x/views/partials/.DS_Store b/test/3.x/views/partials/.DS_Store new file mode 100644 index 0000000..daf4ed2 Binary files /dev/null and b/test/3.x/views/partials/.DS_Store differ diff --git a/test/3.x/views/partials/partial1.html b/test/3.x/views/partials/partial1.html new file mode 100644 index 0000000..cf2c71b --- /dev/null +++ b/test/3.x/views/partials/partial1.html @@ -0,0 +1 @@ +Test Partial 1 \ No newline at end of file diff --git a/test/3.x/views/partials/partial2.html b/test/3.x/views/partials/partial2.html new file mode 100644 index 0000000..b18a251 --- /dev/null +++ b/test/3.x/views/partials/partial2.html @@ -0,0 +1 @@ +Test Partial 2 \ No newline at end of file