Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Forked from benw/load-hbs-partials.js
Created October 31, 2017 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shopglobal/073bf5a47193678dc4173c2088cf22e0 to your computer and use it in GitHub Desktop.
Save shopglobal/073bf5a47193678dc4173c2088cf22e0 to your computer and use it in GitHub Desktop.
Loads partial handlebars templates from files in a directory
// Helps with this problem:
// http://stackoverflow.com/questions/8059914/express-js-hbs-module-register-partials-from-hbs-file
var hbs = require('hbs');
var fs = require('fs');
var partialsDir = __dirname + '/../views/partials';
var filenames = fs.readdirSync(partialsDir);
filenames.forEach(function (filename) {
var matches = /^([^.]+).hbs$/.exec(filename);
if (!matches) {
return;
}
var name = matches[1];
var template = fs.readFileSync(partialsDir + '/' + filename, 'utf8');
hbs.registerPartial(name, template);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment