@@ -14,7 +14,7 @@ var url = require('url')
14
14
* @api public
15
15
*/
16
16
17
- function helpers ( name ) {
17
+ function helpers ( name , app ) {
18
18
return function ( req , res , next ) {
19
19
res . locals . appName = name || 'App'
20
20
res . locals . title = name || 'App'
@@ -34,6 +34,33 @@ function helpers (name) {
34
34
res . locals . warning = req . flash ( 'warning' )
35
35
}
36
36
37
+ /**
38
+ * Render mobile views
39
+ *
40
+ * If the request is coming from a mobile/tablet device, it will check if
41
+ * there is a .mobile.ext file and it that exists it tries to render it.
42
+ *
43
+ * Refer https://github.com/madhums/nodejs-express-mongoose-demo/issues/39
44
+ * For the implementation refer the above app
45
+ */
46
+
47
+ var ua = req . header ( 'user-agent' )
48
+ var fs = require ( 'fs' )
49
+
50
+ res . _render = res . render
51
+ req . isMobile = / m o b i l e / i. test ( ua )
52
+
53
+ res . render = function ( template , locals , cb ) {
54
+ var view = template + '.mobile.' + app . get ( 'view engine' )
55
+ var file = app . get ( 'views' ) + '/' + view
56
+
57
+ if ( / m o b i l e / i. test ( ua ) && fs . existsSync ( file ) ) {
58
+ res . _render ( view , locals , cb )
59
+ } else {
60
+ res . _render ( template , locals , cb )
61
+ }
62
+ }
63
+
37
64
next ( )
38
65
}
39
66
}
0 commit comments