Skip to content
Myr

view.js

This file

Path

lib/view.js

175 lines · 3.1 KiB · JavaScript

Last changed by

Add debug message when loading view enginePravdomilbefore adoptionfefd729

History of this file

Take it

The walk stopped after 200 commits: lines older than 2ac2509 point at it.

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 1–77why, one step back
1/*!
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');19var utils = require('./utils');2021/**22 * Module variables.23 * @private24 */2526var dirname = path.dirname;27var basename = path.basename;28var extname = path.extname;29var join = path.join;30var resolve = path.resolve;3132/**33 * Module exports.34 * @public35 */3637module.exports = View;3839/**40 * Initialize a new `View` with the given `name`.41 *42 * Options:43 *44 * - `defaultEngine` the default template engine name45 * - `engines` template engine require() cache46 * - `root` root path for view lookup47 *48 * @param {string} name49 * @param {object} options50 * @public51 */5253function View(name, options) {54 var opts = options || {};5556 this.defaultEngine = opts.defaultEngine;57 this.ext = extname(name);58 this.name = name;59 this.root = opts.root;6061 if (!this.ext && !this.defaultEngine) {62 throw new Error('No default engine was specified and no extension was provided.');63 }6465 var fileName = name;6667 if (!this.ext) {68 // get extension from default engine name69 this.ext = this.defaultEngine[0] !== '.'70 ? '.' + this.defaultEngine71 : this.defaultEngine;7273 fileName += this.ext;74 }7576 if (!opts.engines[this.ext]) {77 // load engine

Add debug message when loading view enginePravdomilbefore adoptionfefd729

lines 78–80why, one step back
78 var mod = this.ext.substr(1)
79 debug('require "%s"', mod)80 opts.engines[this.ext] = require(mod).__express

4.13.1Douglas Christopher Wilsonbefore adoption2ac2509

lines 81–175why, one step back
81 }
8283 // store loaded engine84 this.engine = opts.engines[this.ext];8586 // lookup path87 this.path = this.lookup(fileName);88}8990/**91 * Lookup view by the given `name`92 *93 * @param {string} name94 * @private95 */9697View.prototype.lookup = function lookup(name) {98 var path;99 var roots = [].concat(this.root);100101 debug('lookup "%s"', name);102103 for (var i = 0; i < roots.length && !path; i++) {104 var root = roots[i];105106 // resolve the path107 var loc = resolve(root, name);108 var dir = dirname(loc);109 var file = basename(loc);110111 // resolve the file112 path = this.resolve(dir, file);113 }114115 return path;116};117118/**119 * Render with the given options.120 *121 * @param {object} options122 * @param {function} callback123 * @private124 */125126View.prototype.render = function render(options, callback) {127 debug('render "%s"', this.path);128 this.engine(this.path, options, callback);129};130131/**132 * Resolve the file within the given directory.133 *134 * @param {string} dir135 * @param {string} file136 * @private137 */138139View.prototype.resolve = function resolve(dir, file) {140 var ext = this.ext;141142 // <path>.<ext>143 var path = join(dir, file);144 var stat = tryStat(path);145146 if (stat && stat.isFile()) {147 return path;148 }149150 // <path>/index.<ext>151 path = join(dir, basename(file, ext), 'index' + ext);152 stat = tryStat(path);153154 if (stat && stat.isFile()) {155 return path;156 }157};158159/**160 * Return a stat, maybe.161 *162 * @param {string} path163 * @return {fs.Stats}164 * @private165 */166167function tryStat(path) {168 debug('stat "%s"', path);169170 try {171 return fs.statSync(path);172 } catch (e) {173 return undefined;174 }175}