diff --git a/README.md b/README.md index 0f08d12..85b1d0a 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,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') @@ -96,4 +96,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 27177a3..5431be4 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,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; @@ -76,8 +76,7 @@ module.exports = function(){ // done next(); - } -} + }; /*** * Allow to register a specific rendering @@ -104,7 +103,7 @@ var register = function(ext,render) { } }; -module.exports.register = register; +r.register = register; /** * Automatically assign a render() function @@ -121,7 +120,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. @@ -143,7 +142,7 @@ var cache = {}; * @api private */ -function resolveObjectName(view){ +var resolveObjectName = function(view){ return cache[view] || (cache[view] = view .split('/') .slice(-1)[0] @@ -171,7 +170,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 @@ -219,7 +218,7 @@ function lookup(root, view, ext){ * @api public */ -function partial(view, options){ +var partial = function(view, options){ var collection , object , locals @@ -325,4 +324,7 @@ function partial(view, options){ } else { return render(); } +}; + + return r; } 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(); + }) + }) + }) + +}) diff --git a/test/test.partials.js b/test/test.partials.js index b15f589..bb4b094 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){