Created
January 19, 2014 03:01
-
-
Save milosdakic/8499961 to your computer and use it in GitHub Desktop.
Express.js multi folder views
This file contains 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 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