From b51fc1841e663fc0e7dc42b41f262542765ff0b2 Mon Sep 17 00:00:00 2001 From: Stephane Alnet Date: Mon, 25 Jun 2012 21:59:47 +0200 Subject: [PATCH 1/2] Made `register` bound to a specific Express instance. Instead of changing the settings at the module level, we allow the user to create a new instance of `partials` for each Express instance. This avoid issues with conflicting settings when multiple Express engines are running concurrently. In that case the usage would then be: var express_partials = require('express-partials'); /* express1 uses jade */ express1.set('view engine','jade'); partials1 = express_partials(); partials1.register('html','jade'); express1.use(partials1); /* express2 uses ejs */ express2.set('view engine','ejs'); partials2 = express_partials(); partials2.register('html','ejs'); express2.use(partials1); Without this patch, the latest `register('html',...')` called would override the options for both. --- README.md | 6 +++--- index.js | 18 ++++++++++-------- test/test.partials.js | 4 ++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b3d07b1..9c06708 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ The simple case: ```javascript var express = require('express') - , partials = require('express-partials') + , partials = require('express-partials')() , app = express(); // load the express-partials middleware -app.use(partials()); +app.use(partials); app.get('/',function(req,res,next){ res.render('index.ejs') @@ -95,4 +95,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/index.js b/index.js index 7e99ba9..6d4a19e 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ var path = require('path') */ module.exports = function(){ - return function(req,res,next){ + var r = function(req,res,next){ // res.partial(view,options) -> res.render() (ignores any layouts) res.partial = res.render; @@ -71,8 +71,7 @@ module.exports = function(){ // done next(); - } -} + }; /*** * Allow to register a specific rendering @@ -99,7 +98,7 @@ var register = function(ext,render) { } }; -module.exports.register = register; +r.register = register; /** * Automatically assign a render() function @@ -116,7 +115,7 @@ var renderer = function(ext) { : register[ext] = require(ext.slice(1)).render; }; -module.exports.renderer = renderer; +r.renderer = renderer; /** * Memory cache for resolved object names. @@ -138,7 +137,7 @@ var cache = {}; * @api private */ -function resolveObjectName(view){ +var resolveObjectName = function(view){ return cache[view] || (cache[view] = view .split('/') .slice(-1)[0] @@ -166,7 +165,7 @@ function resolveObjectName(view){ * @api private */ -function lookup(root, view, ext){ +var lookup = function(root, view, ext){ var name = resolveObjectName(view); // Try _ prefix ex: ./views/_.jade @@ -214,7 +213,7 @@ function lookup(root, view, ext){ * @api public */ -function partial(view, options){ +var partial = function(view, options){ var collection , object , locals @@ -320,4 +319,7 @@ function partial(view, options){ } else { return render(); } +}; + + return r; } diff --git a/test/test.partials.js b/test/test.partials.js index 3c1ef5c..329117d 100644 --- a/test/test.partials.js +++ b/test/test.partials.js @@ -1,9 +1,9 @@ var express = require('express') , request = require('./support/http') - , partials = require('../'); + , partials = require('../')(); var app = express(); -app.use(partials()); +app.use(partials); app.set('views',__dirname + '/fixtures') app.locals.use(function(req,res){ From bf9d79b43950730dcadd237b8eee536016da64dd Mon Sep 17 00:00:00 2001 From: Stephane Alnet Date: Mon, 25 Jun 2012 22:35:02 +0200 Subject: [PATCH 2/2] Added test with concurrent Express 3.x processes with different partial options. --- test/fixtures/ejs/index.html | 1 + test/fixtures/ejs/layout.html | 1 + test/fixtures/jade/index.html | 1 + test/fixtures/jade/layout.html | 5 ++++ test/test.parallel.js | 55 ++++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 test/fixtures/ejs/index.html create mode 100644 test/fixtures/ejs/layout.html create mode 100644 test/fixtures/jade/index.html create mode 100644 test/fixtures/jade/layout.html create mode 100644 test/test.parallel.js diff --git a/test/fixtures/ejs/index.html b/test/fixtures/ejs/index.html new file mode 100644 index 0000000..2e924a7 --- /dev/null +++ b/test/fixtures/ejs/index.html @@ -0,0 +1 @@ +

ejs says hello <%- hello -%>

diff --git a/test/fixtures/ejs/layout.html b/test/fixtures/ejs/layout.html new file mode 100644 index 0000000..bf27b5a --- /dev/null +++ b/test/fixtures/ejs/layout.html @@ -0,0 +1 @@ +ejs layout<%- body %> diff --git a/test/fixtures/jade/index.html b/test/fixtures/jade/index.html new file mode 100644 index 0000000..5cf7a19 --- /dev/null +++ b/test/fixtures/jade/index.html @@ -0,0 +1 @@ +h2= 'Jade says hello ' + hello diff --git a/test/fixtures/jade/layout.html b/test/fixtures/jade/layout.html new file mode 100644 index 0000000..ac3bb99 --- /dev/null +++ b/test/fixtures/jade/layout.html @@ -0,0 +1,5 @@ +html + head + title Jade layout + body + != body diff --git a/test/test.parallel.js b/test/test.parallel.js new file mode 100644 index 0000000..e7b0726 --- /dev/null +++ b/test/test.parallel.js @@ -0,0 +1,55 @@ +var express = require('express') + , request = require('./support/http') + , express_partials = require('../'); + +/* app1: .html files are ejs files */ +var app1 = express(); +var partials1 = express_partials(); +app1.use(partials1); +app1.set('views',__dirname + '/fixtures/ejs') +app1.set('view engine','html') +app1.engine('html',require('ejs').__express) +partials1.register('html','ejs') + +app1.get('/',function(req,res,next){ + res.render('index.html',{hello:'world'}) +}) + +/* app2: .html files are jade files */ +var app2 = express(); +var partials2 = express_partials(); +app2.use(partials2); +app2.set('views',__dirname + '/fixtures/jade') +app2.set('view engine','html') +app2.engine('html',require('jade').__express) +partials2.register('html','jade') +app2.get('/',function(req,res,next){ + res.render('index.html',{hello:'world'}) +}) + +describe('app',function(){ + describe('GET app1 /',function(){ + it('should render using ejs',function(done){ + request(app1) + .get('/') + .end(function(res){ + res.should.have.status(200); + res.body.should.equal('ejs layout

ejs says hello world

\n'); + done(); + }) + }) + }) + + describe('GET app2 /',function(){ + it('should render using Jade',function(done){ + request(app2) + .get('/') + .end(function(res){ + res.should.have.status(200); + res.body.should.equal('Jade layout

Jade says hello world

'); + done(); + }) + }) + }) + +})