Started View implementation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent c8ddedb · Browse files · 2 files·+229 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+229 added−5 removed
- tests:+104 added−1 removed
- other:+125 added−4 removed
Provenance
- Commit
9f4b736- Tree
af5d830- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
c8ddedb
2 files changed+229 added−5 removed
modified:lib/express/view.js+125 added-4 removed
8 unmodified lines9 * Module dependencies.10 */11 12 var extname = require('path').extname13+ , dirname = require('path').dirname14+ , basename = require('path').basename15 , utils = require('connect').utils16 , http = require('http')17 , fs = require('fs')18 , mime = utils.mime;19 20 /**19- * Cache supported template engine exports to20- * increase performance by lowering the number21- * of calls to `require()`.22- * 21+ * Memory cache.22+ *23 * @type Object24 */25 26 var cache = {};14 unmodified lines41 42 var viewNameCache = {};43 44 /**45+ * Initialize a new `View` with the given `view` path,46+ * `options` and callback `fn`.47+ *48+ * @param {String} view49+ * @param {Object} options50+ * @param {Function} fn51+ * @api private52+ */53+54+var View = exports = module.exports = function View(view, options, fn) {55+ options = options || {};56+ // TODO: memoize basename etc57+ this.view = view;58+ this.fn = fn;59+ this.root = options.root;60+ this.partial = options.partial;61+ this.defaultEngine = options.defaultViewEngine;62+ this.parent = options.parentView;63+ this.basename = basename(view);64+ this.engine = this.resolveEngine();65+ this.extension = '.' + this.engine;66+ this.path = this.resolvePath();67+ this.dirname = dirname(this.path);68+ if (this.partial) {69+ this.objectName = options.as || this.resolveObjectName();70+ this.path = this.dirname + '/_' + basename(this.path);71+ }72+};73+74+/**75+ * Resolve view engine.76+ *77+ * @return {String}78+ * @api private79+ */80+81+View.prototype.resolveEngine = function(){82+ // Explicit83+ if (~this.basename.indexOf('.')) return extname(this.basename).substr(1);84+ // Inherit from parent85+ if (this.parent) return this.parent.engine;86+ // Default87+ return this.defaultEngine;88+};89+90+/**91+ * Resolve view path.92+ *93+ * @return {String}94+ * @api private95+ */96+97+View.prototype.resolvePath = function(){98+ var path = this.view;99+ // Implicit engine100+ if (!~this.basename.indexOf('.')) path += this.extension;101+ // Absolute102+ if ('/' == path[0]) return path;103+ // Relative to parent104+ if (this.parent) return this.parent.dirname + '/' + path;105+ // Relative to root106+ return this.root107+ ? this.root + '/' + path108+ : path;109+};110+111+/**112+ * Resolve partial object name from the view path.113+ *114+ * Examples:115+ *116+ * "user.ejs" becomes "user"117+ * "forum thread.ejs" becomes "forumThread"118+ * "forum/thread/post.ejs" becomes "post"119+ * "blog-post.ejs" becomes "blogPost"120+ *121+ * @return {String}122+ * @api private123+ */124+125+View.prototype.resolveObjectName = function(){126+ // TODO: cache127+ return this.view128+ .split('/')129+ .slice(-1)[0]130+ .split('.')[0]131+ .replace(/[^a-zA-Z0-9 ]+/g, ' ')132+ .split(/ +/).map(function(word, i){133+ return i134+ ? word[0].toUpperCase() + word.substr(1)135+ : word;136+ }).join('');137+};138+139+/**140+ * Get view contents. This is a one-time hit, so we141+ * can afford to be sync.142+ *143+ * @return {String}144+ * @api public145+ */146+147+View.prototype.__defineGetter__('contents', function(){148+ return fs.readFileSync(this.view, 'utf8');149+});150+151+/**152+ * Get template engine api, cache exports to reduce153+ * require() calls.154+ *155+ * @return {Object}156+ * @api public157+ */158+159+View.prototype.__defineGetter__('templateEngine', function(){160+ var engine = this.engine;161+ return cache[engine]162+ || (cache[engine] = require(engine))163+});164+165+/**166 * Synchronously cache view at the given `path`.167 *168 * @param {String} path169 * @return {String}303 unmodified linesmodified:test/view.test.js+104 added-1 removed
5 unmodified lines6 var express = require('express')7 , connect = require('connect')8 , assert = require('assert')9 , should = require('should')10- , view = require('express/view');10+ , View = require('express/view');11 12 /**13 * Create a test server with views set to ./fixtures.14 */4 unmodified lines19 return app;20 };21 22 module.exports = {23+ 'test View#path': function(){24+ var view = new View('forum/thread.ejs', { root: '/www/mysite/views' });25+ view.path.should.equal('/www/mysite/views/forum/thread.ejs');26+27+ var view = new View('/www/mysite/views/path.ejs');28+ view.path.should.equal('/www/mysite/views/path.ejs');29+30+ var view = new View('user', { parentView: view });31+ view.path.should.equal('/www/mysite/views/user.ejs');32+33+ var view = new View('user/list', { parentView: view });34+ view.path.should.equal('/www/mysite/views/user/list.ejs');35+36+ var view = new View('user.jade', { parentView: new View('foo', { root: '/bar' }) });37+ view.path.should.equal('/bar/user.jade');38+39+ var view = new View('/foo.bar.baz/user.ejs');40+ view.path.should.equal('/foo.bar.baz/user.ejs');41+ 42+ var view = new View('/foo.bar.baz/user', { parentView: view });43+ view.path.should.equal('/foo.bar.baz/user.ejs');44+45+ var view = new View('user', { parentView: view });46+ view.path.should.equal('/foo.bar.baz/user.ejs');47+ },48+ 49+ 'test View#path for partials': function(){50+ var view = new View('user.ejs', { root: '/www', partial: true });51+ view.path.should.equal('/www/_user.ejs');52+53+ var view = new View('user', { parentView: view, root: '/www', partial: true });54+ view.path.should.equal('/www/_user.ejs');55+ 56+ var view = new View('forum/thread', { parentView: view, partial: true });57+ view.path.should.equal('/www/forum/_thread.ejs');58+59+ var view = new View('forum/thread.jade', { root: '/www', partial: true });60+ view.path.should.equal('/www/forum/_thread.jade');61+ },62+ 63+ 'test View#engine': function(){64+ var view = new View('/absolute/path.ejs');65+ view.engine.should.equal('ejs');66+67+ var view = new View('user', { parentView: view });68+ view.engine.should.equal('ejs');69+70+ var view = new View('/user', { defaultViewEngine: 'jade' });71+ view.engine.should.equal('jade');72+73+ var view = new View('/foo.bar/user.ejs');74+ view.engine.should.equal('ejs');75+ },76+ 77+ 'test View#extension': function(){78+ var view = new View('/absolute/path.ejs');79+ view.extension.should.equal('.ejs');80+81+ var view = new View('user', { parentView: view });82+ view.extension.should.equal('.ejs');83+84+ var view = new View('/user', { defaultViewEngine: 'jade' });85+ view.extension.should.equal('.jade');86+87+ var view = new View('/foo.bar/user.ejs');88+ view.extension.should.equal('.ejs');89+ },90+ 91+ 'test View#dirname': function(){92+ var view = new View('/absolute/path.ejs');93+ view.dirname.should.equal('/absolute');94+95+ var view = new View('user', { parentView: view });96+ view.dirname.should.equal('/absolute');97+ },98+ 99+ 'test View#objectName': function(){100+ var view = new View('/path/to/user.ejs', { partial: true });101+ view.objectName.should.equal('user');102+103+ var view = new View('/path/to/user-post.ejs', { partial: true });104+ view.objectName.should.equal('userPost');105+106+ var view = new View('/path/to/user_post.ejs', { partial: true });107+ view.objectName.should.equal('userPost');108+109+ var view = new View('/path/to/forum thread post.ejs', { partial: true });110+ view.objectName.should.equal('forumThreadPost');111+ 112+ var view = new View('/path/to/forum thread post.ejs', { as: 'post', partial: true });113+ view.objectName.should.equal('post');114+ },115+ 116+ 'test View#contents': function(){117+ var view = new View(__dirname + '/fixtures/hello.jade');118+ view.contents.should.equal('p :(');119+ },120+ 121+ 'test View#templateEngine': function(){122+ var view = new View(__dirname + '/fixtures/hello.jade');123+ view.templateEngine.should.equal(require('jade'));124+ },125+126 'test #render()': function(){127 var app = create();128 app.set('view engine', 'jade');129 657 unmodified lines