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 6bc7574 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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 14–15why, one step back
14 */
15

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 16why, one step back
16var debug = require('debug')('express:view');

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 17–19why, one step back
17var path = require('path');
18var fs = require('fs');19var utils = require('./utils');

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

lines 20–25why, one step back
20
21/**22 * Module variables.23 * @private24 */25

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 26–29why, one step back
26var dirname = path.dirname;
27var basename = path.basename;28var extname = path.extname;29var join = path.join;

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 30why, one step back
30var resolve = path.resolve;

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 31–32why, one step back
31
32/**

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

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);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

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);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 86–88why, one step back
86}
8788/**

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

line 89why, one step back
89 * Lookup view by the given `name`

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

line 90why, one step back
90 *

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 93–94why, one step back
93 */
94

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 95–97why, one step back
95View.prototype.lookup = function lookup(name) {
96 var path;97 var roots = [].concat(this.root);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

line 98why, one step back
98

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

line 99why, one step back
99 debug('lookup "%s"', name);

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 100why, one step back
100

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 101–102why, one step back
101 for (var i = 0; i < roots.length && !path; i++) {
102 var root = roots[i];

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 103why, one step back
103

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 104–107why, one step back
104 // resolve the path
105 var loc = resolve(root, name);106 var dir = dirname(loc);107 var file = basename(loc);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

line 108why, one step back
108

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 109–113why, one step back
109 // resolve the file
110 path = this.resolve(dir, file);111 }112113 return path;

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 114–116why, one step back
114};
115116/**

perf: remove argument reassignments in ViewDouglas Christopher Wilsonbefore adoptiona8a8564

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

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) {

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

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);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

line 127why, one step back
127};

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

lines 128–140why, one step back
128
129/**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);

Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352

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}