Skip to content
Myr

view.js

This file

Path

lib/view.js

77 lines · 1.8 KiB · JavaScript

Last changed by

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

History of this file

Take it

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 5–12why, one step back
5var path = require('path');
6var fs = require('fs');7var utils = require('./utils');8var dirname = path.dirname;9var basename = path.basename;10var extname = path.extname;11var exists = fs.existsSync || path.existsSync;12var join = path.join;

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 13–77why, one step back
13
14/**15 * Expose `View`.16 */1718module.exports = View;1920/**21 * Initialize a new `View` with the given `name`.22 *23 * Options:24 *25 * - `defaultEngine` the default template engine name26 * - `engines` template engine require() cache27 * - `root` root path for view lookup28 *29 * @param {String} name30 * @param {Object} options31 * @api private32 */3334function View(name, options) {35 options = options || {};36 this.name = name;37 this.root = options.root;38 var engines = options.engines;39 this.defaultEngine = options.defaultEngine;40 var ext = this.ext = extname(name);41 if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.');42 if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine);43 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);44 this.path = this.lookup(name);45}4647/**48 * Lookup view by the given `path`49 *50 * @param {String} path51 * @return {String}52 * @api private53 */5455View.prototype.lookup = function(path){56 var ext = this.ext;5758 // <path>.<engine>59 if (!utils.isAbsolute(path)) path = join(this.root, path);60 if (exists(path)) return path;6162 // <path>/index.<engine>63 path = join(dirname(path), basename(path, ext), 'index' + ext);64 if (exists(path)) return path;65};6667/**68 * Render with the given `options` and callback `fn(err, str)`.69 *70 * @param {Object} options71 * @param {Function} fn72 * @api private73 */7475View.prototype.render = function(options, fn){76 this.engine(this.path, options, fn);77};