Skip to content
Myr

view.js

This file

Path

lib/view.js

76 lines · 1.6 KiB · JavaScript

Last changed by

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption6ca1807

History of this file

Take it

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

clean up weird whitespace in error-pages acceptance testsTJ Holowaychukbefore adoption2c14d0c

lines 1–5why, one step back
1/**
2 * Module dependencies.3 */45var path = require('path')

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption6ca1807

line 6why, one step back
6 , fs = require('fs')

clean up weird whitespace in error-pages acceptance testsTJ Holowaychukbefore adoption2c14d0c

lines 7–10why, one step back
7 , utils = require('./utils')
8 , dirname = path.dirname9 , basename = path.basename10 , extname = path.extname

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption6ca1807

line 11why, one step back
11 , exists = fs.existsSync || path.existsSync

clean up weird whitespace in error-pages acceptance testsTJ Holowaychukbefore adoption2c14d0c

lines 12–76why, one step back
12 , join = path.join;
1314/**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 name 26 * - `engines` template engine require() cache 27 * - `root` root path for view lookup 28 *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) name += (ext = this.ext = '.' + this.defaultEngine);42 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);43 this.path = this.lookup(name);44}4546/**47 * Lookup view by the given `path`48 *49 * @param {String} path50 * @return {String}51 * @api private52 */5354View.prototype.lookup = function(path){55 var ext = this.ext;5657 // <path>.<engine>58 if (!utils.isAbsolute(path)) path = join(this.root, path);59 if (exists(path)) return path;6061 // <path>/index.<engine>62 path = join(dirname(path), basename(path, ext), 'index' + ext);63 if (exists(path)) return path;64};6566/**67 * Render with the given `options` and callback `fn(err, str)`.68 *69 * @param {Object} options70 * @param {Function} fn71 * @api private72 */7374View.prototype.render = function(options, fn){75 this.engine(this.path, options, fn);76};