Skip to content
Myr

moved View and Partial into their own files

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 8dc341d · Browse files · 3 files+173 added−132 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+173 added−132 removed

  • other:+173 added−132 removed

3 files+173 added−132 removed

added:lib/express/view/partial.js+61 added
added:lib/express/view/view.js+106 added
12/*!3 * Express - View4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var extname = require('path').extname13 , dirname = require('path').dirname14 , basename = require('path').basename15 , fs = require('fs');1617/**18 * Memory cache.19 */2021var cache = {};2223/**24 * Initialize a new `View` with the given `view` path and `options`.25 *26 * @param {String} view27 * @param {Object} options28 * @api private29 */3031var View = exports = module.exports = function View(view, options) {32 options = options || {};33 // TODO: memoize basename etc34 this.view = view;35 this.root = options.root;36 this.relative = false !== options.relative;37 this.defaultEngine = options.defaultEngine;38 this.parent = options.parentView;39 this.basename = basename(view);40 this.engine = this.resolveEngine();41 this.extension = '.' + this.engine;42 this.path = this.resolvePath();43 this.dirname = dirname(this.path);44};4546/**47 * Resolve view engine.48 *49 * @return {String}50 * @api private51 */5253View.prototype.resolveEngine = function(){54 // Explicit55 if (~this.basename.indexOf('.')) return extname(this.basename).substr(1);56 // Inherit from parent57 if (this.parent) return this.parent.engine;58 // Default59 return this.defaultEngine;60};6162/**63 * Resolve view path.64 *65 * @return {String}66 * @api private67 */6869View.prototype.resolvePath = function(){70 var path = this.view;71 // Implicit engine72 if (!~this.basename.indexOf('.')) path += this.extension;73 // Absolute74 if ('/' == path[0]) return path;75 // Relative to parent76 if (this.relative && this.parent) return this.parent.dirname + '/' + path;77 // Relative to root78 return this.root79 ? this.root + '/' + path80 : path;81};8283/**84 * Get view contents. This is a one-time hit, so we85 * can afford to be sync.86 *87 * @return {String}88 * @api public89 */9091View.prototype.__defineGetter__('contents', function(){92 return fs.readFileSync(this.path, 'utf8');93});9495/**96 * Get template engine api, cache exports to reduce97 * require() calls.98 *99 * @return {Object}100 * @api public101 */102103View.prototype.__defineGetter__('templateEngine', function(){104 var ext = this.extension;105 return cache[ext] || (cache[ext] = require(this.engine));106});
modified:lib/express/view.js+6 added-132 removed
12/*!3 * Express - View3 * Express - view4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */7
5 unmodified lines
13 , dirname = require('path').dirname14 , basename = require('path').basename15 , utils = require('connect').utils16 , clone = require('./utils').clone17 , View = require('./view/view')18 , Partial = require('./view/partial')19 , merge = utils.merge20 , http = require('http')19 , fs = require('fs')21 , mime = utils.mime;2223/**24 * Memory cache.
3 unmodified lines
2829var cache = {};3031/**31 * Initialize a new `View` with the given `view` path and `options`.32 *33 * @param {String} view34 * @param {Object} options35 * @api private32 * Expose constructors.33 */3438var View = exports = module.exports = function View(view, options) {39 options = options || {};40 // TODO: memoize basename etc41 this.view = view;42 this.root = options.root;43 this.relative = false !== options.relative;44 this.defaultEngine = options.defaultEngine;45 this.parent = options.parentView;46 this.basename = basename(view);47 this.engine = this.resolveEngine();48 this.extension = '.' + this.engine;49 this.path = this.resolvePath();50 this.dirname = dirname(this.path);51};35exports = module.exports = View;36exports.Partial = Partial;3738/**54 * Resolve view engine.55 *56 * @return {String}57 * @api private58 */5960View.prototype.resolveEngine = function(){61 // Explicit62 if (~this.basename.indexOf('.')) return extname(this.basename).substr(1);63 // Inherit from parent64 if (this.parent) return this.parent.engine;65 // Default66 return this.defaultEngine;67};6869/**70 * Resolve view path.71 *72 * @return {String}73 * @api private74 */7576View.prototype.resolvePath = function(){77 var path = this.view;78 // Implicit engine79 if (!~this.basename.indexOf('.')) path += this.extension;80 // Absolute81 if ('/' == path[0]) return path;82 // Relative to parent83 if (this.relative && this.parent) return this.parent.dirname + '/' + path;84 // Relative to root85 return this.root86 ? this.root + '/' + path87 : path;88};8990/**91 * Get view contents. This is a one-time hit, so we92 * can afford to be sync.93 *94 * @return {String}95 * @api public96 */9798View.prototype.__defineGetter__('contents', function(){99 return fs.readFileSync(this.path, 'utf8');100});101102/**103 * Get template engine api, cache exports to reduce104 * require() calls.105 *106 * @return {Object}107 * @api public108 */109110View.prototype.__defineGetter__('templateEngine', function(){111 var ext = this.extension;112 return cache[ext]113 || (cache[ext] = require(this.engine));114});115116/**117 * Initialize a new `Partial` for the given `view` and `options`.118 *119 * @param {String} view120 * @param {Object} options121 * @api private122 */123124var Partial = exports.Partial = function Partial(view, options) {125 options = options || {};126 View.call(this, view, options);127 this.path = this.dirname + '/_' + basename(this.path);128};129130/**131 * Inherit from `View.prototype`.132 */133134Partial.prototype.__proto__ = View.prototype;135136/**137 * Resolve partial object name from the view path.138 *139 * Examples:140 *141 * "user.ejs" becomes "user"142 * "forum thread.ejs" becomes "forumThread"143 * "forum/thread/post.ejs" becomes "post"144 * "blog-post.ejs" becomes "blogPost"145 *146 * @return {String}147 * @api private148 */149150Partial.resolveObjectName = function(view){151 // TODO: cache152 return view153 .split('/')154 .slice(-1)[0]155 .split('.')[0]156 .replace(/[^a-zA-Z0-9 ]+/g, ' ')157 .split(/ +/).map(function(word, i){158 return i159 ? word[0].toUpperCase() + word.substr(1)160 : word;161 }).join('');162};163164/**39 * Register the given template engine `exports`40 * as `ext`. For example we may wish to map ".html"41 * files to jade:42 *
231 unmodified lines