Skip to content

Commit

Permalink
fixing #10 with app.set('views') being an array
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Lambert authored and JacksonTian committed Jun 8, 2015
1 parent fdc74be commit 4cbcb9b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
16 changes: 15 additions & 1 deletion index.js
Expand Up @@ -115,7 +115,21 @@ var renderFile = module.exports = function(file, options, fn){
delete options.filename;

if (layout.length > 0) {
layout = join(options.settings.views, layout);
var views = options.settings.views;
var l = layout;

if (!Array.isArray(views)) {
views = [views];
}

for (var i = 0; i < views.length; i++) {
layout = join(views[i], l);

// use the first found layout
if (exists(layout)) {
break;
}
}
}

// now recurse and use the current result as `body` in the layout:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"ejs": "1.0.0"
},
"devDependencies": {
"express": "~3.0.0",
"express": "~4.10.0",
"mocha": "*",
"should": "~3.0.0",
"methods": "0.0.1"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/thing/views-array.ejs
@@ -0,0 +1 @@
<h1>Views Array</h1>
4 changes: 1 addition & 3 deletions test/test.partials.js
Expand Up @@ -11,9 +11,7 @@ app.engine('ejs', engine);
// if you want to load `layout.ejs` as the default layout
// (this was the default in Express 2.0 so it's handy for
// quick ports and upgrades)
app.locals({
_layoutFile: true,
})
app.locals._layoutFile = true;

app.locals.hello = 'there';

Expand Down
50 changes: 50 additions & 0 deletions test/test.views-array.js
@@ -0,0 +1,50 @@
var express = require('express')
, request = require('./support/http')
, engine = require('../')
, ejs = require('ejs')

var app = express();
app.set('views',[__dirname + '/fixtures', __dirname + '/fixtures/thing']);
app.engine('ejs', engine);

// this is not the default behavior, but you can set this
// if you want to load `layout.ejs` as the default layout
// (this was the default in Express 2.0 so it's handy for
// quick ports and upgrades)
app.locals._layoutFile = true;

app.get('/views-array',function(req,res,next){
res.render('index.ejs',{_layoutFile:false})
})

app.get('/views-array-thing',function(req,res,next){
res.render('views-array.ejs')
})

describe('app with views array',function(){

describe('GET /views-array', function(){
it('should render index.ejs from /fixtures',function(done){
request(app)
.get('/views-array')
.end(function(res){
res.should.have.status(200);
res.body.should.equal('<h1>Index</h1>');
done();
})
})
})

describe('GET /views-array-thing', function(){
it('should render views-array.ejs from /fixtures/thing',function(done){
request(app)
.get('/views-array-thing')
.end(function(res){
res.should.have.status(200);
res.body.should.equal('<html><head><title>ejs-locals</title></head><body><h1>Views Array</h1></body></html>');
done();
})
})
})

})

0 comments on commit 4cbcb9b

Please sign in to comment.