From ead5c8de82608825e5281b2046012df0ad4a26ec Mon Sep 17 00:00:00 2001 From: Alex Gray Date: Fri, 5 Aug 2016 20:02:43 -0400 Subject: [PATCH 1/5] accomodated multiple 'views' roots in lookup --- index.js | 58 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 0693fde..19db641 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ var path = require('path') * The beloved feature from Express 2.x is back as a middleware. * * Example: - * + * * var express = require('express') * , partials = require('express-partials') * , app = express(); @@ -25,7 +25,7 @@ var path = require('path') * app.get('/',function(req,res,next){ * res.render('index.ejs') // renders layout.ejs with index.ejs as `body`. * }) - * + * * Options: * * none @@ -51,7 +51,7 @@ module.exports = function(){ var viewOptions = res.app.get('view options'); layout = viewOptions && viewOptions.defaultLayout || 'layout'; } - + // layout if( layout ){ // first render normally @@ -98,7 +98,7 @@ module.exports = function(){ } } -/*** +/*** * Allow to register a specific rendering * function for a given extension. * (Similar to Express 2.x register() function.) @@ -194,33 +194,35 @@ function lookup(root, view, ext){ var name = resolveObjectName(view); var original = view; - // Try root ex: /user.jade - view = resolve(root, basename(original,ext)+ext); - if( exists(view) ) return view; + (typeof root == 'string' ? [root] : root).some(function(root) { + // Try root ex: /user.jade + view = resolve(root, basename(original,ext)+ext); + if( exists(view) ) return true; - // Try subdir ex: /subdir/user.jade - view = resolve(root, dirname(original), basename(original,ext)+ext); - if( exists(view) ) return view; + // Try subdir ex: /subdir/user.jade + view = resolve(root, dirname(original), basename(original,ext)+ext); + if( exists(view) ) return true; - // Try _ prefix ex: ./views/_.jade - // taking precedence over the direct path - view = resolve(root,'_'+name+ext) - if( exists(view) ) return view; + // Try _ prefix ex: ./views/_.jade + // taking precedence over the direct path + view = resolve(root,'_'+name+ext) + if( exists(view) ) return true; - // Try index ex: ./views/user/index.jade - view = resolve(root,name,'index'+ext); - if( exists(view) ) return view; + // Try index ex: ./views/user/index.jade + view = resolve(root,name,'index'+ext); + if( exists(view) ) return true; - // Try ..//index ex: ../user/index.jade - // when calling partial('user') within the same dir - view = resolve(root,'..',name,'index'+ext); - if( exists(view) ) return view; + // Try ..//index ex: ../user/index.jade + // when calling partial('user') within the same dir + view = resolve(root,'..',name,'index'+ext); + if( exists(view) ) return true; - // Try root ex: /user.jade - view = resolve(root,name+ext); - if( exists(view) ) return view; + // Try root ex: /user.jade + view = resolve(root,name+ext); + if( exists(view) ) return true; + }); - return null; + return exists(view) ? view : null; }; module.exports.lookup = lookup; @@ -286,11 +288,11 @@ function partial(view, options){ if( locals ) options.__proto__ = locals; - // merge app locals into + // merge app locals into for(var k in this.app.locals) options[k] = options[k] || this.app.locals[k]; - // merge locals, which as set using app.use(function(...){ res.locals = X; }) + // merge locals, which as set using app.use(function(...){ res.locals = X; }) for(var k in this.req.res.locals) options[k] = options[k] || this.req.res.locals[k]; @@ -304,7 +306,7 @@ function partial(view, options){ var root = this.app.get('views') || process.cwd() + '/views' , ext = extname(view) || '.' + (this.app.get('view engine')||'ejs') , file = lookup(root, view, ext); - + // read view var source = fs.readFileSync(file,'utf8'); From 2d087bae00843e47ab1f65b42decc1cf87215032 Mon Sep 17 00:00:00 2001 From: Alex Gray Date: Fri, 5 Aug 2016 20:14:19 -0400 Subject: [PATCH 2/5] undid what my editor did to the trailing whitespace --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 19db641..e3c1966 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ var path = require('path') * The beloved feature from Express 2.x is back as a middleware. * * Example: - * + * * var express = require('express') * , partials = require('express-partials') * , app = express(); @@ -25,7 +25,7 @@ var path = require('path') * app.get('/',function(req,res,next){ * res.render('index.ejs') // renders layout.ejs with index.ejs as `body`. * }) - * + * * Options: * * none @@ -51,7 +51,7 @@ module.exports = function(){ var viewOptions = res.app.get('view options'); layout = viewOptions && viewOptions.defaultLayout || 'layout'; } - + // layout if( layout ){ // first render normally @@ -98,7 +98,7 @@ module.exports = function(){ } } -/*** +/*** * Allow to register a specific rendering * function for a given extension. * (Similar to Express 2.x register() function.) @@ -288,11 +288,11 @@ function partial(view, options){ if( locals ) options.__proto__ = locals; - // merge app locals into + // merge app locals into for(var k in this.app.locals) options[k] = options[k] || this.app.locals[k]; - // merge locals, which as set using app.use(function(...){ res.locals = X; }) + // merge locals, which as set using app.use(function(...){ res.locals = X; }) for(var k in this.req.res.locals) options[k] = options[k] || this.req.res.locals[k]; @@ -306,7 +306,7 @@ function partial(view, options){ var root = this.app.get('views') || process.cwd() + '/views' , ext = extname(view) || '.' + (this.app.get('view engine')||'ejs') , file = lookup(root, view, ext); - + // read view var source = fs.readFileSync(file,'utf8'); From 6fe903af4c232163afe1853b571c58382a8d0949 Mon Sep 17 00:00:00 2001 From: Alex Gray Date: Tue, 9 Aug 2016 10:52:06 -0400 Subject: [PATCH 3/5] express 4.1.0 req'd for these revisions --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 4d36f37..e2a7d23 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,14 @@ "dependencies": { }, "devDependencies": { - "express": "3.0.0", + "express": "latest", "methods": "*", "mocha": "*", "supertest": "0.4.0", "ejs": "*", "hamljs": "*", "jade": "*", - "eco": "*", + "eco": "latest", "coffeecup": "*", "consolidate": "*" }, From ec725428a219a83e8d3cd7e0bd2abe5f6ae850e3 Mon Sep 17 00:00:00 2001 From: Alex Gray Date: Tue, 9 Aug 2016 10:53:07 -0400 Subject: [PATCH 4/5] migrated existing tests to be 4.x compatible. quite easy --- test/fixtures/chained-views/app.js | 20 +++++++++----------- test/fixtures/default-layout/app.js | 14 +++++++------- test/fixtures/register/app.js | 8 +++----- test/fixtures/subdir/app.js | 11 +++++------ 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/test/fixtures/chained-views/app.js b/test/fixtures/chained-views/app.js index bb654b7..4d1cabf 100644 --- a/test/fixtures/chained-views/app.js +++ b/test/fixtures/chained-views/app.js @@ -2,23 +2,21 @@ var express = require('express') , partials = require('../../../') , app = module.exports = express(); -app.configure(function() { - app.set('views', __dirname); - app.set('view engine', 'ejs'); +app.set('views', __dirname); +app.set('view engine', 'ejs'); - app.use(function(req, res, next){ - res.locals.logged_user = 'bob'; - res.locals.config = {title:'partially'}; - next(); - }) - - app.use(partials()) +app.use(function(req, res, next){ + res.locals.logged_user = 'bob'; + res.locals.config = {title:'partially'}; + next(); }) +app.use(partials()) + app.get('/', function(req, res){ res.render('view1') }) app.get('/locals', function(req, res){ res.render('locals1') -}) \ No newline at end of file +}) diff --git a/test/fixtures/default-layout/app.js b/test/fixtures/default-layout/app.js index bc0bacd..c1f649f 100644 --- a/test/fixtures/default-layout/app.js +++ b/test/fixtures/default-layout/app.js @@ -2,12 +2,12 @@ var express = require('express') , partials = require('../../../') , app = module.exports = express(); -app.configure(function() { - app.set('views', __dirname); - app.set('view engine', 'ejs'); - app.set('view options', {defaultLayout: 'layout-1'}); - app.use(partials()); -}) + +app.set('views', __dirname); +app.set('view engine', 'ejs'); +app.set('view options', {defaultLayout: 'layout-1'}); +app.use(partials()); + app.get('/1', function(req, res){ res.render('view'); @@ -15,4 +15,4 @@ app.get('/1', function(req, res){ app.get('/2', function(req, res){ res.render('view', {layout: 'layout-2'}); -}) \ No newline at end of file +}) diff --git a/test/fixtures/register/app.js b/test/fixtures/register/app.js index cbd5f10..bc0a479 100644 --- a/test/fixtures/register/app.js +++ b/test/fixtures/register/app.js @@ -4,11 +4,9 @@ var express = require('express') , jade = require('jade') , app = module.exports = express(); -app.configure(function() { - app.set('views', __dirname); - app.set('view engine', 'ejs'); - app.use(partials()); -}) +app.set('views', __dirname); +app.set('view engine', 'ejs'); +app.use(partials()); /* Use `register` to substitute the file extension. */ app.engine('.j',jade.__express); diff --git a/test/fixtures/subdir/app.js b/test/fixtures/subdir/app.js index 69458f3..543988b 100644 --- a/test/fixtures/subdir/app.js +++ b/test/fixtures/subdir/app.js @@ -2,11 +2,10 @@ var express = require('express') , partials = require('../../../') , app = module.exports = express(); -app.configure(function() { - app.set('views', __dirname); - app.set('view engine', 'ejs'); - app.use(partials()); -}) + +app.set('views', __dirname); +app.set('view engine', 'ejs'); +app.use(partials()); app.get('/subdir',function(req,res,next){ res.render('subdir/index.ejs') @@ -32,4 +31,4 @@ app.get('/subdir-a-layout',function(req,res,next){ app.get('/subdir-aView',function(req,res,next){ res.render('subdir/aView.ejs') -}) \ No newline at end of file +}) From 7d4b02fbe88e5864007fb6a265f096575c75c4ea Mon Sep 17 00:00:00 2001 From: Alex Gray Date: Tue, 9 Aug 2016 10:54:06 -0400 Subject: [PATCH 5/5] implemented passing tests that probe functionality of multiple 'views' roots in coffeescript, even --- test/fixtures/multiple-search-paths/app.js | 24 ++++++++++++++++++++++ test/test.partials.multipath.js | 15 ++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/fixtures/multiple-search-paths/app.js create mode 100644 test/test.partials.multipath.js diff --git a/test/fixtures/multiple-search-paths/app.js b/test/fixtures/multiple-search-paths/app.js new file mode 100644 index 0000000..c4293ef --- /dev/null +++ b/test/fixtures/multiple-search-paths/app.js @@ -0,0 +1,24 @@ +var express = require('express') + , partials = require('../../../') + , path = require('path') + , app = module.exports = express() + , cc = require('coffeecup') + , regDir = path.join(path.dirname(__dirname),'register'); + + +/* CoffeeCup is supported, at least in Express 4.x. + * It is not handled by consolidate, so we need to register it. + * express-partials uses CoffeeCup's render() automatically. + */ + +app.use(partials()); +app.set('view engine', 'coffeecup'); +app.set('views', [__dirname, regDir]); +app.engine('coffeecup', cc.__express); +app.set('view options', { defaultLayout: path.join(regDir,'layout.coffeecup') }); +partials.register('coffeecup', cc); + + +app.get('/coffeecup',function(req,res,next){ + res.render('index', {hello:'world'}) +}) diff --git a/test/test.partials.multipath.js b/test/test.partials.multipath.js new file mode 100644 index 0000000..2a84bdc --- /dev/null +++ b/test/test.partials.multipath.js @@ -0,0 +1,15 @@ +var app = require('./fixtures/multiple-search-paths/app') + , request = require('supertest') + , partials = require('../'); + +describe('app',function(){ + describe('GET /coffeecup',function(){ + it('should find resolve and render index.coffeecup as a CoffeeCup template with layout.coffeecup as CoffeeCup layout',function(done){ + request(app) + .get('/coffeecup') + .expect(200) + .expect('CoffeeCup layout

CoffeeCup says hello world

') + .end(done) + }) + }) +})