view.js
This file
The walk stopped after 200 commits: lines older than fa272ed point at it.
docs: fix typo in jsdoc commentHrvoje Šimićbefore adoptionfa272ed
lines 1–182why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */89'use strict';1011/**12 * Module dependencies.13 * @private14 */1516var debug = require('debug')('express:view');17var path = require('path');18var fs = require('fs');1920/**21 * Module variables.22 * @private23 */2425var dirname = path.dirname;26var basename = path.basename;27var extname = path.extname;28var join = path.join;29var resolve = path.resolve;3031/**32 * Module exports.33 * @public34 */3536module.exports = View;3738/**39 * Initialize a new `View` with the given `name`.40 *41 * Options:42 *43 * - `defaultEngine` the default template engine name44 * - `engines` template engine require() cache45 * - `root` root path for view lookup46 *47 * @param {string} name48 * @param {object} options49 * @public50 */5152function View(name, options) {53 var opts = options || {};5455 this.defaultEngine = opts.defaultEngine;56 this.ext = extname(name);57 this.name = name;58 this.root = opts.root;5960 if (!this.ext && !this.defaultEngine) {61 throw new Error('No default engine was specified and no extension was provided.');62 }6364 var fileName = name;6566 if (!this.ext) {67 // get extension from default engine name68 this.ext = this.defaultEngine[0] !== '.'69 ? '.' + this.defaultEngine70 : this.defaultEngine;7172 fileName += this.ext;73 }7475 if (!opts.engines[this.ext]) {76 // load engine77 var mod = this.ext.substr(1)78 debug('require "%s"', mod)7980 // default engine export81 var fn = require(mod).__express8283 if (typeof fn !== 'function') {84 throw new Error('Module "' + mod + '" does not provide a view engine.')85 }8687 opts.engines[this.ext] = fn88 }8990 // store loaded engine91 this.engine = opts.engines[this.ext];9293 // lookup path94 this.path = this.lookup(fileName);95}9697/**98 * Lookup view by the given `name`99 *100 * @param {string} name101 * @private102 */103104View.prototype.lookup = function lookup(name) {105 var path;106 var roots = [].concat(this.root);107108 debug('lookup "%s"', name);109110 for (var i = 0; i < roots.length && !path; i++) {111 var root = roots[i];112113 // resolve the path114 var loc = resolve(root, name);115 var dir = dirname(loc);116 var file = basename(loc);117118 // resolve the file119 path = this.resolve(dir, file);120 }121122 return path;123};124125/**126 * Render with the given options.127 *128 * @param {object} options129 * @param {function} callback130 * @private131 */132133View.prototype.render = function render(options, callback) {134 debug('render "%s"', this.path);135 this.engine(this.path, options, callback);136};137138/**139 * Resolve the file within the given directory.140 *141 * @param {string} dir142 * @param {string} file143 * @private144 */145146View.prototype.resolve = function resolve(dir, file) {147 var ext = this.ext;148149 // <path>.<ext>150 var path = join(dir, file);151 var stat = tryStat(path);152153 if (stat && stat.isFile()) {154 return path;155 }156157 // <path>/index.<ext>158 path = join(dir, basename(file, ext), 'index' + ext);159 stat = tryStat(path);160161 if (stat && stat.isFile()) {162 return path;163 }164};165166/**167 * Return a stat, maybe.168 *169 * @param {string} path170 * @return {fs.Stats}171 * @private172 */173174function tryStat(path) {175 debug('stat "%s"', path);176177 try {178 return fs.statSync(path);179 } catch (e) {180 return undefined;181 }182}