From 4cbcb9ba6799066be22bbed7bb2574fdcc7aff1b Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Sat, 6 Jun 2015 20:49:47 -0600 Subject: [PATCH] fixing #10 with app.set('views') being an array --- index.js | 16 ++++++++- package.json | 2 +- test/fixtures/thing/views-array.ejs | 1 + test/test.partials.js | 4 +-- test/test.views-array.js | 50 +++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/thing/views-array.ejs create mode 100644 test/test.views-array.js diff --git a/index.js b/index.js index 773e399..d75b699 100644 --- a/index.js +++ b/index.js @@ -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: diff --git a/package.json b/package.json index 9610af0..dd94284 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/fixtures/thing/views-array.ejs b/test/fixtures/thing/views-array.ejs new file mode 100644 index 0000000..2c6388f --- /dev/null +++ b/test/fixtures/thing/views-array.ejs @@ -0,0 +1 @@ +

Views Array

\ No newline at end of file diff --git a/test/test.partials.js b/test/test.partials.js index e3bf973..bc5077f 100644 --- a/test/test.partials.js +++ b/test/test.partials.js @@ -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'; diff --git a/test/test.views-array.js b/test/test.views-array.js new file mode 100644 index 0000000..3f78c42 --- /dev/null +++ b/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('

Index

'); + 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('ejs-locals

Views Array

'); + done(); + }) + }) + }) + +}) \ No newline at end of file