Add support for app.set('views', array)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #2320
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 0e5f2f8 · Browse files · 8 files·+180 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+180 added−15 removed
- tests:+109 added−0 removed
- other:+71 added−15 removed
Provenance
- Commit
6614352- Tree
39a31a2- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
0e5f2f8
8 files changed+180 added−15 removed
modified:lib/application.js+4 added-1 removed
511 unmodified lines512 engines: engines513 });514 515 if (!view.path) {516+ var dirs = Array.isArray(view.root) && view.root.length > 1517+ ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'518+ : 'directory "' + view.root + '"'516- var err = new Error('Failed to lookup view "' + name + '" in views directory "' + view.root + '"');519+ var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);520 err.view = view;521 return fn(err);522 }523 48 unmodified linesmodified:lib/view.js+65 added-14 removed
13 unmodified lines14151617181819202129 unmodified lines51525354565556585758596061636465626364656766676970686970727371727374757576777677787980818283846 unmodified lines91929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114213 unmodified lines
var dirname = path.dirname;var basename = path.basename;var extname = path.extname;var exists = fs.existsSync || path.existsSync;var join = path.join;var resolve = path.resolve;
/**29 unmodified lines this.path = this.lookup(name);}
/** * Lookup view by the given `path` * Lookup view by the given `name` * * @param {String} path * @param {String} name * @return {String} * @api private */
View.prototype.lookup = function lookup(path) { var ext = this.ext; var root = this.root;View.prototype.lookup = function lookup(name) { var path; var roots = [].concat(this.root);
debug('lookup "%s"', path); debug('lookup "%s"', name);
// resolve the path path = resolve(root, path); for (var i = 0; i < roots.length && !path; i++) { var root = roots[i];
// <path>.<engine> if (exists(path)) return path; // resolve the path var loc = resolve(root, name); var dir = dirname(loc); var file = basename(loc);
// <path>/index.<engine> path = join(dirname(path), basename(path, ext), 'index' + ext); if (exists(path)) return path; // resolve the file path = this.resolve(dir, file); }
return path;};
/** * Render with the given `options` and callback `fn(err, str)`.6 unmodified linesView.prototype.render = function render(options, fn) { debug('render "%s"', this.path); this.engine(this.path, options, fn);};
/** * Resolve the file within the given directory. * * @param {string} dir * @param {string} file * @private */
View.prototype.resolve = function resolve(dir, file) { var ext = this.ext; var path; var stat;
// <path>.<ext> path = join(dir, file); stat = tryStat(path);
if (stat && stat.isFile()) { return path; }
// <path>/index.<ext> path = join(dir, basename(file, ext), 'index' + ext); stat = tryStat(path);
if (stat && stat.isFile()) { return path; }};
/** * Return a stat, maybe. * * @param {string} path * @return {fs.Stats} * @private */
function tryStat(path) { debug('stat "%s"', path);
try { return fs.statSync(path); } catch (e) { return undefined; }}added:test/fixtures/default_layout/name.jade+1 added
1+p= nameadded:test/fixtures/default_layout/user.jade+1 added
1+p= user.nameadded:test/fixtures/local_layout/user.jade+1 added
1+span= user.namemodified:test/app.render.js+58 added
129 unmodified lines130 })131 })132 })133 134+ describe('when "views" is given', function(){135+ it('should lookup the file in the path', function(done){136+ var app = express();137+138+ app.set('views', __dirname + '/fixtures/default_layout');139+ app.locals.user = { name: 'tobi' };140+141+ app.render('user.jade', function(err, str){142+ if (err) return done(err);143+ str.should.equal('<p>tobi</p>');144+ done();145+ })146+ })147+148+ describe('when array of paths', function(){149+ it('should lookup the file in the path', function(done){150+ var app = express();151+ var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];152+153+ app.set('views', views);154+ app.locals.user = { name: 'tobi' };155+156+ app.render('user.jade', function(err, str){157+ if (err) return done(err);158+ str.should.equal('<span>tobi</span>');159+ done();160+ })161+ })162+163+ it('should lookup in later paths until found', function(done){164+ var app = express();165+ var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];166+167+ app.set('views', views);168+ app.locals.name = 'tobi';169+170+ app.render('name.jade', function(err, str){171+ if (err) return done(err);172+ str.should.equal('<p>tobi</p>');173+ done();174+ })175+ })176+177+ it('should error if file does not exist', function(done){178+ var app = express();179+ var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];180+181+ app.set('views', views);182+ app.locals.name = 'tobi';183+184+ app.render('pet.jade', function(err, str){185+ err.message.should.equal('Failed to lookup view "pet.jade" in views directories "' + __dirname + '/fixtures/local_layout" or "' + __dirname + '/fixtures/default_layout"');186+ done();187+ })188+ })189+ })190+ })191+192 describe('when a "view" constructor is given', function(){193 it('should create an instance of it', function(done){194 var app = express();195 157 unmodified linesmodified:test/res.render.js+48 added
112 unmodified lines113 .get('/')114 .expect('<p>This is an email</p>', done);115 })116 })117+118+ describe('when "views" is given', function(){119+ it('should lookup the file in the path', function(done){120+ var app = express();121+122+ app.set('views', __dirname + '/fixtures/default_layout');123+124+ app.use(function(req, res){125+ res.render('user.jade', { user: { name: 'tobi' } });126+ });127+128+ request(app)129+ .get('/')130+ .expect('<p>tobi</p>', done);131+ })132+133+ describe('when array of paths', function(){134+ it('should lookup the file in the path', function(done){135+ var app = express();136+ var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];137+138+ app.set('views', views);139+140+ app.use(function(req, res){141+ res.render('user.jade', { user: { name: 'tobi' } });142+ });143+144+ request(app)145+ .get('/')146+ .expect('<span>tobi</span>', done);147+ })148+149+ it('should lookup in later paths until found', function(done){150+ var app = express();151+ var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];152+153+ app.set('views', views);154+155+ app.use(function(req, res){156+ res.render('name.jade', { name: 'tobi' });157+ });158+159+ request(app)160+ .get('/')161+ .expect('<p>tobi</p>', done);162+ })163+ })164+ })165 })166 167 describe('.render(name, option)', function(){168 it('should render the template', function(done){152 unmodified linesmodified:History.md+2 added
1 unreleased2 ==========3 4+ * Add support for `app.set('views', array)`5+ - Views are looked up in sequence in array of directories6 * Fix `res.send(status)` to mention `res.sendStatus(status)`7 * Use `content-disposition` module for `res.attachment`/`res.download`8 - Sends standards-compliant `Content-Disposition` header9 - Full Unicode support2425 unmodified lines