view.js
This file
Path
lib/view/view.js
209 lines · 4.1 KiB · JavaScript
Last changed by
Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e
History of this fileTake it
The walk stopped after 200 commits: lines older than 99b49d2 point at it.
updated docsTj Holowaychukbefore adoption99b49d2
lines 1–11why, one step back12/*!3 * Express - View4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */11Merge branch 'feature/router'Tj Holowaychukbefore adoption1d2dd2a
lines 12–15why, one step back12var path = require('path')13 , extname = path.extname14 , dirname = path.dirname15 , basename = path.basenameupdated docsTj Holowaychukbefore adoption99b49d2
lines 16–19why, one step back16 , fs = require('fs')17 , stat = fs.statSync;1819/**Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
lines 20–25why, one step back20 * Expose `View`.21 */2223exports = module.exports = View;2425/**Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
line 26why, one step back26 * Require cache.updated docsTj Holowaychukbefore adoption99b49d2
lines 27–38why, one step back27 */2829var cache = {};3031/**32 * Initialize a new `View` with the given `view` path and `options`.33 *34 * @param {String} view35 * @param {Object} options36 * @api private37 */38Merge branch 'refactor/router'Tj Holowaychukbefore adoption351f6ab
line 39why, one step back39function View(view, options) {updated docsTj Holowaychukbefore adoption99b49d2
lines 40–51why, one step back40 options = options || {};41 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.name = this.basename.replace(this.extension, '');50 this.path = this.resolvePath();51 this.dirname = dirname(this.path);Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e
lines 52–55why, one step back52 if (options.attempts) {53 if (!~options.attempts.indexOf(this.path))54 options.attempts.push(this.path);55 }updated docsTj Holowaychukbefore adoption99b49d2
lines 56–65why, one step back56};5758/**59 * Check if the view path exists.60 *61 * @return {Boolean}62 * @api public63 */6465View.prototype.__defineGetter__('exists', function(){Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 66–70why, one step back66 try {67 stat(this.path);68 return true;69 } catch (err) {70 return false;updated docsTj Holowaychukbefore adoption99b49d2
lines 71–209why, one step back71 }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.relative && this.parent) return this.parent.dirname + '/' + path;105 // Relative to root106 return this.root107 ? this.root + '/' + path108 : path;109};110111/**112 * Get view contents. This is a one-time hit, so we113 * can afford to be sync.114 *115 * @return {String}116 * @api public117 */118119View.prototype.__defineGetter__('contents', function(){120 return fs.readFileSync(this.path, 'utf8');121});122123/**124 * Get template engine api, cache exports to reduce125 * require() calls.126 *127 * @return {Object}128 * @api public129 */130131View.prototype.__defineGetter__('templateEngine', function(){132 var ext = this.extension;133 return cache[ext] || (cache[ext] = require(this.engine));134});135136/**137 * Return root path alternative.138 *139 * @return {String}140 * @api public141 */142143View.prototype.__defineGetter__('rootPath', function(){144 this.relative = false;145 return this.resolvePath();146});147148/**149 * Return index path alternative.150 *151 * @return {String}152 * @api public153 */154155View.prototype.__defineGetter__('indexPath', function(){156 return this.dirname157 + '/' + this.basename.replace(this.extension, '')158 + '/index' + this.extension;159});160161/**162 * Return ../<name>/index path alternative.163 *164 * @return {String}165 * @api public166 */167168View.prototype.__defineGetter__('upIndexPath', function(){169 return this.dirname + '/../' + this.name + '/index' + this.extension;170});171172/**173 * Return _ prefix path alternative174 *175 * @return {String}176 * @api public177 */178179View.prototype.__defineGetter__('prefixPath', function(){180 return this.dirname + '/_' + this.basename;181});182183/**184 * Register the given template engine `exports`185 * as `ext`. For example we may wish to map ".html"186 * files to jade:187 *188 * app.register('.html', require('jade'));189 *190 * or191 *192 * app.register('html', require('jade'));193 *194 * This is also useful for libraries that may not195 * match extensions correctly. For example my haml.js196 * library is installed from npm as "hamljs" so instead197 * of layout.hamljs, we can register the engine as ".haml":198 *199 * app.register('.haml', require('haml-js'));200 *201 * @param {String} ext202 * @param {Object} obj203 * @api public204 */205206exports.register = function(ext, exports) {207 if ('.' != ext[0]) ext = '.' + ext;208 cache[ext] = exports;209};