Skip to content
Myr

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

2 files+229 added−5 removed

modified:lib/express/view.js+125 added-4 removed
8 unmodified lines
9 * Module dependencies.10 */1112var 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;1920/**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 */2526var cache = {};
14 unmodified lines
4142var viewNameCache = {};4344/**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 */5354var 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};7374/**75 * Resolve view engine.76 *77 * @return {String}78 * @api private79 */8081View.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};8990/**91 * Resolve view path.92 *93 * @return {String}94 * @api private95 */9697View.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};110111/**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 */124125View.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};138139/**140 * Get view contents. This is a one-time hit, so we141 * can afford to be sync.142 *143 * @return {String}144 * @api public145 */146147View.prototype.__defineGetter__('contents', function(){148 return fs.readFileSync(this.view, 'utf8');149});150151/**152 * Get template engine api, cache exports to reduce153 * require() calls.154 *155 * @return {Object}156 * @api public157 */158159View.prototype.__defineGetter__('templateEngine', function(){160 var engine = this.engine;161 return cache[engine]162 || (cache[engine] = require(engine))163});164165/**166 * Synchronously cache view at the given `path`.167 *168 * @param {String} path169 * @return {String}
303 unmodified lines
modified:test/view.test.js+104 added-1 removed
5 unmodified lines
6var 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');1112/**13 * Create a test server with views set to ./fixtures.14 */
4 unmodified lines
19 return app;20};2122module.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');2627 var view = new View('/www/mysite/views/path.ejs');28 view.path.should.equal('/www/mysite/views/path.ejs');2930 var view = new View('user', { parentView: view });31 view.path.should.equal('/www/mysite/views/user.ejs');3233 var view = new View('user/list', { parentView: view });34 view.path.should.equal('/www/mysite/views/user/list.ejs');3536 var view = new View('user.jade', { parentView: new View('foo', { root: '/bar' }) });37 view.path.should.equal('/bar/user.jade');3839 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');4445 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');5253 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');5859 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');6667 var view = new View('user', { parentView: view });68 view.engine.should.equal('ejs');6970 var view = new View('/user', { defaultViewEngine: 'jade' });71 view.engine.should.equal('jade');7273 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');8081 var view = new View('user', { parentView: view });82 view.extension.should.equal('.ejs');8384 var view = new View('/user', { defaultViewEngine: 'jade' });85 view.extension.should.equal('.jade');8687 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');9495 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');102103 var view = new View('/path/to/user-post.ejs', { partial: true });104 view.objectName.should.equal('userPost');105106 var view = new View('/path/to/user_post.ejs', { partial: true });107 view.objectName.should.equal('userPost');108109 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 },125126 'test #render()': function(){127 var app = create();128 app.set('view engine', 'jade');129
657 unmodified lines