Skip to content
Myr

view.js

This file

Path

lib/view.js

173 lines · 3.1 KiB · JavaScript

Last changed by

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

History of this file

Take it

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

docs: add license commentsDouglas Christopher Wilsonbefore adoption95ad276

lines 1–8why, 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 */8

perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f

lines 9–10why, one step back
9'use strict';
10

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 11–12why, one step back
11/**
12 * Module dependencies.

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

line 13why, one step back
13 * @private

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 14–32why, one step back
14 */
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/**

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 33–34why, one step back
33 * Module exports.
34 * @public

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 35–47why, one step back
35 */
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 *

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 48–50why, one step back
48 * @param {string} name
49 * @param {object} options50 * @public

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 51–53why, one step back
51 */
5253function View(name, options) {

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 54–57why, one step back
54 var opts = options || {};
5556 this.defaultEngine = opts.defaultEngine;57 this.ext = extname(name);

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

line 58why, one step back
58 this.name = name;

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 59–85why, one step back
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 engine78 opts.engines[this.ext] = require(this.ext.substr(1)).__express;79 }8081 // store loaded engine82 this.engine = opts.engines[this.ext];8384 // lookup path85 this.path = this.lookup(fileName);

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 86–90why, one step back
86}
8788/**89 * Lookup view by the given `name`90 *

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 91–92why, one step back
91 * @param {string} name
92 * @private

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 93–116why, one step back
93 */
9495View.prototype.lookup = function lookup(name) {96 var path;97 var roots = [].concat(this.root);9899 debug('lookup "%s"', name);100101 for (var i = 0; i < roots.length && !path; i++) {102 var root = roots[i];103104 // resolve the path105 var loc = resolve(root, name);106 var dir = dirname(loc);107 var file = basename(loc);108109 // resolve the file110 path = this.resolve(dir, file);111 }112113 return path;114};115116/**

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

line 117why, one step back
117 * Render with the given options.

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

line 118why, one step back
118 *

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 119–121why, one step back
119 * @param {object} options
120 * @param {function} callback121 * @private

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 122–123why, one step back
122 */
123

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

line 124why, one step back
124View.prototype.render = function render(options, callback) {

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

line 125why, one step back
125 debug('render "%s"', this.path);

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

line 126why, one step back
126 this.engine(this.path, options, callback);

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 127–140why, one step back
127};
128129/**130 * Resolve the file within the given directory.131 *132 * @param {string} dir133 * @param {string} file134 * @private135 */136137View.prototype.resolve = function resolve(dir, file) {138 var ext = this.ext;139140 // <path>.<ext>

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

lines 141–142why, one step back
141 var path = join(dir, file);
142 var stat = tryStat(path);

deps: type-is@~1.6.0Douglas Christopher Wilsonbefore adoptione03ed05

lines 143–173why, one step back
143
144 if (stat && stat.isFile()) {145 return path;146 }147148 // <path>/index.<ext>149 path = join(dir, basename(file, ext), 'index' + ext);150 stat = tryStat(path);151152 if (stat && stat.isFile()) {153 return path;154 }155};156157/**158 * Return a stat, maybe.159 *160 * @param {string} path161 * @return {fs.Stats}162 * @private163 */164165function tryStat(path) {166 debug('stat "%s"', path);167168 try {169 return fs.statSync(path);170 } catch (e) {171 return undefined;172 }173}