Skip to content

Instantly share code, notes, and snippets.

@milosdakic
Created January 19, 2014 03:01
Show Gist options
  • Save milosdakic/8499961 to your computer and use it in GitHub Desktop.
Save milosdakic/8499961 to your computer and use it in GitHub Desktop.
Express.js multi folder views
var path = require('path')
, fs = require('fs')
, utils = require('./utils')
, dirname = path.dirname
, basename = path.basename
, extname = path.extname
, exists = fs.existsSync || path.existsSync
, join = path.join;
module.exports = function(app) {
var old = app.get('view').prototype.lookup;
var lookup = function(path) {
// If root is an array of paths, let's try each path until we find the view
if (this.root instanceof Array) {
this.root.forEach(function(directory) {
if (!utils.isAbsolute(path)) path = join(directory, path);
if (exists(path)) return path;
});
}
// Fallback to standard behavior, when root is a single directory
return old.call(this, path);
};
return old = lookup;
}
// Usage
var views = app.get('views');
views.push(__dirname + '/views')
app.set('views', views);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment