ejs-locals
Advanced tools
Comparing version 0.1.6 to 0.2.0
38
index.js
@@ -63,3 +63,3 @@ var ejs = require('ejs') | ||
var renderFile = module.exports = function(path, options, fn){ | ||
var renderFile = module.exports = function(file, options, fn){ | ||
@@ -87,3 +87,3 @@ // Express used to set options.locals for us, but now we do it ourselves | ||
ejs.renderFile(path, options, function(err, html) { | ||
ejs.renderFile(file, options, function(err, html) { | ||
@@ -106,10 +106,12 @@ if (err) { | ||
// use default extension | ||
var engine = options.settings['view engine'] || 'ejs', | ||
desiredExt = '.'+engine; | ||
// apply default layout if only "true" was set | ||
if (layout === true) { | ||
layout = 'layout.ejs'; | ||
layout = path.sep + 'layout' + desiredExt; | ||
} | ||
// apply default extension | ||
if (extname(layout) != '.ejs') { | ||
// FIXME: how to reach 'view engine' from here? | ||
layout += '.ejs'; | ||
if (extname(layout) !== desiredExt) { | ||
layout += desiredExt; | ||
} | ||
@@ -123,6 +125,13 @@ | ||
// find layout path relative to current template, then | ||
// recurse and use this layout as `body` in the parent | ||
if (layout.length > 0 && layout[0] === path.sep) { | ||
// if layout is an absolute path, find it relative to view options: | ||
layout = join(options.settings.views, layout.slice(1)); | ||
} else { | ||
// otherwise, find layout path relative to current template: | ||
layout = resolve(dirname(file), layout); | ||
} | ||
// now recurse and use the current result as `body` in the layout: | ||
options.locals.body = html; | ||
renderFile(join(dirname(path), layout), options, fn); | ||
renderFile(layout, options, fn); | ||
} else { | ||
@@ -189,3 +198,6 @@ // no layout, just do the default: | ||
function lookup(root, partial, options){ | ||
var ext = extname(partial) || '.ejs' // FIXME: reach 'view engine' from here? | ||
var engine = options.settings['view engine'] || 'ejs' | ||
, desiredExt = '.' + engine | ||
, ext = extname(partial) || desiredExt | ||
, key = [ root, partial, ext ].join('-'); | ||
@@ -401,4 +413,6 @@ | ||
var root = dirname(this.filename) | ||
, engine = this.settings['view engine'] || 'ejs' | ||
, desiredExt = '.' + engine | ||
, ext = extname(view) | ||
, file = join(root, view + (ext ? '' : '.ejs')) | ||
, file = join(root, view + (ext ? '' : desiredExt)) | ||
, key = file + ':string'; | ||
@@ -405,0 +419,0 @@ |
@@ -8,3 +8,3 @@ { | ||
"description": "Express 3.x locals for layout, partial and blocks.", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -11,0 +11,0 @@ "type": "git", |
@@ -85,2 +85,10 @@ var express = require('express') | ||
app.get('/subfolder/subitem',function(req,res,next){ | ||
res.render('subfolder/subitem.ejs'); | ||
}); | ||
app.get('/subfolder/subitem-with-layout',function(req,res,next){ | ||
res.render('subfolder/subitem-with-layout.ejs'); | ||
}); | ||
app.get('/non-existent-partial',function(req,res,next){ | ||
@@ -289,2 +297,26 @@ res.render('non-existent-partial.ejs'); | ||
describe('GET /subfolder/subitem',function(){ | ||
it('should render subfolder/subitem.ejs and still use layout.ejs',function(done){ | ||
request(app) | ||
.get('/subfolder/subitem') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<html><head><title>ejs-locals</title></head><body><h1>Index</h1></body></html>'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
describe('GET /subfolder/subitem-with-layout',function(){ | ||
it('should render subitem-with-layout.ejs using sub-layout.ejs',function(done){ | ||
request(app) | ||
.get('/subfolder/subitem-with-layout') | ||
.end(function(res){ | ||
res.should.have.status(200); | ||
res.body.should.equal('<html><head><title>ejs-locals sub-layout</title></head><body><h1>Index</h1></body></html>'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
describe('GET /non-existent-partial',function(){ | ||
@@ -291,0 +323,0 @@ it('should send 500 and error saying a partial was not found',function(done){ |
35218
31
813