Comparing version 2.1.0 to 2.3.0
@@ -12,2 +12,3 @@ | ||
hbs.registerPartial('partial', fs.readFileSync(__dirname + '/views/partial.hbs', 'utf8')); | ||
hbs.registerPartials(__dirname + '/views/partials'); | ||
@@ -14,0 +15,0 @@ // set the view engine to use handlebars |
@@ -6,2 +6,5 @@ | ||
// handle simple flow control | ||
var after = require('after'); | ||
// handle async helpers | ||
@@ -163,2 +166,36 @@ var async = require('./async'); | ||
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) { | ||
@@ -165,0 +202,0 @@ this.handlebars.registerHelper(name, function(context) { |
{ | ||
"name": "hbs", | ||
"description": "Express.js template engine plugin for Handlebars", | ||
"version": "2.1.0", | ||
"version": "2.3.0", | ||
"homepage": "https://github.com/donpark/hbs", | ||
@@ -12,3 +12,4 @@ "author": "Don Park <donpark@docuverse.com> (http://blog.docuverse.com)", | ||
"dependencies": { | ||
"handlebars": "1.0.9" | ||
"handlebars": "1.0.9", | ||
"after": "0.7.0" | ||
}, | ||
@@ -15,0 +16,0 @@ "devDependencies": { |
@@ -38,2 +38,19 @@ # hbs [![Build Status](https://secure.travis-ci.org/donpark/hbs.png)](http://travis-ci.org/donpark/hbs) # | ||
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. | ||
@@ -40,0 +57,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14928
23
296
92
2
+ Addedafter@0.7.0
+ Addedafter@0.7.0(transitive)