Skip to content

Instantly share code, notes, and snippets.

@madhums
Last active December 18, 2015 11:59
Show Gist options
  • Save madhums/5779739 to your computer and use it in GitHub Desktop.
Save madhums/5779739 to your computer and use it in GitHub Desktop.
render mobile layouts using express.js
// Render mobile views
// if the request is coming from mobile and if you have a view
// with users.mobile.jade, then that view will be rendered
app.use(function (req, res, next) {
res._render = res.render
res.render = function (template, locals, cb) {
var ua = req.header('user-agent')
var fs = require('fs')
var view = template + '.mobile.' + app.get('view engine')
var file = app.get('views') + '/' + view
// check if the request was from the mobile - thanks to https://gist.github.com/christopherdebeer/1200142
if (/mobile/i.test(ua) && fs.existsSync(file)) {
res._render(view, locals, cb)
} else {
res._render(template, locals, cb)
}
}
next()
})
@madhums
Copy link
Author

madhums commented Jun 14, 2013

for implementation details checkout madhums/node-express-mongoose-demo#39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment