Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created May 27, 2011 15:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save naholyr/995474 to your computer and use it in GitHub Desktop.
Save naholyr/995474 to your computer and use it in GitHub Desktop.
Allow multiple views roots in Express.js
// Usage:
// var express = require('express')
// require('enableMultipleViewRoots')(express)
module.exports = function(express) {
var old = express.view.lookup;
function lookup(view, options) {
// If root is an array of paths, let's try each path until we find the view
if (options.root instanceof Array) {
var opts = {}
for (var key in options) opts[key] = options[key]
var root = opts.root, foundView = null
for (var i=0; i<root.length; i++) {
opts.root = root[i]
foundView = lookup.call(this, view, opts)
if (foundView.exists) break
}
return foundView
}
// Fallback to standard behavior, when root is a single directory
return old.call(express.view, view, options)
}
express.view.lookup = lookup;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment