Skip to content

Instantly share code, notes, and snippets.

@MrBenJ
Created October 23, 2015 22:32
Show Gist options
  • Save MrBenJ/3b933bb9c6b460a03887 to your computer and use it in GitHub Desktop.
Save MrBenJ/3b933bb9c6b460a03887 to your computer and use it in GitHub Desktop.
Algorithm to register all handlebars partials at once on an express server.
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var hbs = require('hbs');
var fs = require('fs');
// ... stuff omitted for brevity
// Register Handlebars Partials
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