@@ -5,10 +5,9 @@ var cheerio = require('cheerio'), // used to convert raw html into jQu
55 NodeCache = require ( "node-cache" ) , // auto expiring in memory caching collection
66 cache = new NodeCache ( { stdTTL : 300 } ) ; // cache source HTML for 5 minutes, after which we fetch a new version
77
8- // override res.render to allow us to fetch template url or use html template string
98module . exports = function ( req , res , next ) {
10-
11- // replace the standard res.render function so we can intercept response rendering
9+
10+ // add res.merge to response object to allow us to fetch template url or use html template string
1211 res . merge = function ( view , model , callback ) {
1312
1413 if ( ! model . sourceUrl ) {
@@ -45,6 +44,7 @@ module.exports = function(req, res, next){
4544
4645 // if the view has a body, use its contents otherwise use the entire content
4746 var $viewBody = $view ( 'body' ) ;
47+
4848 if ( $viewBody && $viewBody . length > 0 ) {
4949 // inject content into selector or body
5050 $template ( sourcePlaceholder ) . html ( $viewBody . html ( ) ) ;
@@ -70,50 +70,50 @@ module.exports = function(req, res, next){
7070
7171/**
7272 * Converts template url to $ object
73- * @param {string } template - url to external template or html content
73+ * @param {string } sourceUrl - url to external template or html content
7474 */
75- function resolveTemplate ( template ) {
75+ function resolveTemplate ( sourceUrl ) {
7676
7777 return new Promise ( function ( resolve , reject ) {
7878
7979 // if its a url and we have the contents in cache
80- if ( template . isUrl ( ) && cache . get ( template ) ) {
80+ if ( sourceUrl . isUrl ( ) && cache . get ( sourceUrl ) ) {
8181
82- // get template html from cache
83- var html = cache . get ( template ) ;
82+ // get source html from cache
83+ var html = cache . get ( sourceUrl ) ;
8484
8585 // covert html into jquery object
8686 var $ = cheerio . load ( html ) ;
8787
88- // return template as a jquery style object
88+ // return source as a jquery style object
8989 resolve ( $ ) ;
9090 }
91- else if ( template . isUrl ( ) ) {
91+ else if ( sourceUrl . isUrl ( ) ) {
9292
93- // request the template url
94- return request ( { uri : template } ) . then ( function ( html ) {
93+ // request the source url
94+ return request ( { uri : sourceUrl } ) . then ( function ( html ) {
9595
9696 // convert html into jquery style object so we can use selectors
9797 var $ = cheerio . load ( html ) ;
9898
99- // add base url to ensure links/scripts/styles load correctly
100- $ ( 'head' ) . prepend ( '<base href="' + template + '">' ) ;
99+ // insert base tag to ensure links/scripts/styles load correctly
100+ $ ( 'head' ) . prepend ( '<base href="' + sourceUrl + '">' ) ;
101101
102102 // cache result as HTML so we dont have to keep getting it for future requests and it remains clean
103- cache . set ( template , $ . html ( ) ) ;
103+ cache . set ( sourceUrl , $ . html ( ) ) ;
104104
105105 // resolve with jquery object containing content
106106 resolve ( $ ) ;
107107 } )
108108 . catch ( function ( err ) {
109109
110110 // request failed
111- reject ( 'Unable to retrieve ' + template ) ;
111+ reject ( 'Unable to retrieve ' + sourceUrl ) ;
112112 } ) ;
113113 }
114114 else {
115- // the template must contain markup, just return it
116- resolve ( template ) ;
115+ // the sourceUrl must contain markup, just return it
116+ resolve ( sourceUrl ) ;
117117 }
118118 } ) ;
119119}
0 commit comments