Created
October 10, 2013 08:36
-
-
Save jonathanconway/6915007 to your computer and use it in GitHub Desktop.
Allows you to customize ExpressJS's view rendering so that you can, say, use views in a 'mobile' folder when user is accessing site through a mobile device.
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
// dependencies: requirejs, express | |
define(['express'], function (express) { | |
return function (app, resolver) { | |
return function(req, res, next) { | |
var oldRender = res.render; | |
res.render = function (view, model) { | |
return oldRender.call(this, resolver.call(this, req, res) + view, model); | |
}; | |
next(); | |
}; | |
}; | |
}); | |
// Example: | |
// If the user is accessing the site from a mobile device, load views from the /mobile subfolder. | |
// Dependencies: mobile-agent, express | |
// var viewResolver = function(req, res) { | |
// if (mobileAgent(req.headers['user-agent']).Mobile) { | |
// return 'mobile/'; | |
// } | |
// return ''; | |
// } | |
// app.use(expressViewResolver(app, viewResolver)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment