Created
October 23, 2015 22:32
-
-
Save MrBenJ/3b933bb9c6b460a03887 to your computer and use it in GitHub Desktop.
Algorithm to register all handlebars partials at once on an express server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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