Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accomodated multiple 'views' roots in lookup #50

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,33 +194,35 @@ function lookup(root, view, ext){
var name = resolveObjectName(view);
var original = view;

// Try root ex: <root>/user.jade
view = resolve(root, basename(original,ext)+ext);
if( exists(view) ) return view;

// Try subdir ex: <root>/subdir/user.jade
view = resolve(root, dirname(original), basename(original,ext)+ext);
if( exists(view) ) return view;

// Try _ prefix ex: ./views/_<name>.jade
// taking precedence over the direct path
view = resolve(root,'_'+name+ext)
if( exists(view) ) return view;

// Try index ex: ./views/user/index.jade
view = resolve(root,name,'index'+ext);
if( exists(view) ) return view;

// Try ../<name>/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 root ex: <root>/user.jade
view = resolve(root,name+ext);
if( exists(view) ) return view;

return null;
(typeof root == 'string' ? [root] : root).some(function(root) {
// Try root ex: <root>/user.jade
view = resolve(root, basename(original,ext)+ext);
if( exists(view) ) return true;

// Try subdir ex: <root>/subdir/user.jade
view = resolve(root, dirname(original), basename(original,ext)+ext);
if( exists(view) ) return true;

// Try _ prefix ex: ./views/_<name>.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 true;

// Try ../<name>/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: <root>/user.jade
view = resolve(root,name+ext);
if( exists(view) ) return true;
});

return exists(view) ? view : null;
};
module.exports.lookup = lookup;

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
},
Expand Down
20 changes: 9 additions & 11 deletions test/fixtures/chained-views/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
14 changes: 7 additions & 7 deletions test/fixtures/default-layout/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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');
})

app.get('/2', function(req, res){
res.render('view', {layout: 'layout-2'});
})
})
24 changes: 24 additions & 0 deletions test/fixtures/multiple-search-paths/app.js
Original file line number Diff line number Diff line change
@@ -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'})
})
8 changes: 3 additions & 5 deletions test/fixtures/register/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions test/fixtures/subdir/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
})
})
15 changes: 15 additions & 0 deletions test/test.partials.multipath.js
Original file line number Diff line number Diff line change
@@ -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('<html><head><title>CoffeeCup layout</title></head><body><h2>CoffeeCup says hello world</h2></body></html>')
.end(done)
})
})
})