Skip to content
Myr

view.js

This file

Path

lib/view.js

142 lines · 2.7 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 1–4why, one step back
1/**
2 * Module dependencies.3 */4

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 6–8why, one step back
6var path = require('path');
7var fs = require('fs');8var utils = require('./utils');

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

lines 9–14why, one step back
9
10/**11 * Module variables.12 * @private13 */14

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 15–18why, one step back
15var dirname = path.dirname;
16var basename = path.basename;17var extname = path.extname;18var join = path.join;

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 20–54why, one step back
20
21/**22 * Expose `View`.23 */2425module.exports = View;2627/**28 * Initialize a new `View` with the given `name`.29 *30 * Options:31 *32 * - `defaultEngine` the default template engine name33 * - `engines` template engine require() cache34 * - `root` root path for view lookup35 *36 * @param {String} name37 * @param {Object} options38 * @api private39 */4041function View(name, options) {42 options = options || {};43 this.name = name;44 this.root = options.root;45 var engines = options.engines;46 this.defaultEngine = options.defaultEngine;47 var ext = this.ext = extname(name);48 if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');49 if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);50 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);51 this.path = this.lookup(name);52}5354/**

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

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

line 56why, one step back
56 *

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

line 57why, one step back
57 * @param {String} name

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 58–61why, one step back
58 * @return {String}
59 * @api private60 */61

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

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

line 65why, one step back
65

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

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

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 67why, one step back
67

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

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

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

line 70why, one step back
70

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

lines 71–74why, one step back
71 // resolve the path
72 var loc = resolve(root, name);73 var dir = dirname(loc);74 var file = basename(loc);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

line 75why, one step back
75

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

lines 76–80why, one step back
76 // resolve the file
77 path = this.resolve(dir, file);78 }7980 return path;

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 81–90why, one step back
81};
8283/**84 * Render with the given `options` and callback `fn(err, str)`.85 *86 * @param {Object} options87 * @param {Function} fn88 * @api private89 */90

Use path.resolve in view lookupDouglas Christopher Wilsonbefore adoption0e5f2f8

lines 91–92why, one step back
91View.prototype.render = function render(options, fn) {
92 debug('render "%s"', this.path);

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 93–94why, one step back
93 this.engine(this.path, options, fn);
94};

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

lines 95–142why, one step back
95
96/**97 * Resolve the file within the given directory.98 *99 * @param {string} dir100 * @param {string} file101 * @private102 */103104View.prototype.resolve = function resolve(dir, file) {105 var ext = this.ext;106 var path;107 var stat;108109 // <path>.<ext>110 path = join(dir, file);111 stat = tryStat(path);112113 if (stat && stat.isFile()) {114 return path;115 }116117 // <path>/index.<ext>118 path = join(dir, basename(file, ext), 'index' + ext);119 stat = tryStat(path);120121 if (stat && stat.isFile()) {122 return path;123 }124};125126/**127 * Return a stat, maybe.128 *129 * @param {string} path130 * @return {fs.Stats}131 * @private132 */133134function tryStat(path) {135 debug('stat "%s"', path);136137 try {138 return fs.statSync(path);139 } catch (e) {140 return undefined;141 }142}