Skip to content
Myr

view.js

This file

Path

lib/view.js

81 lines · 1.7 KiB · JavaScript

Last changed by

Merge pull request #1066 from cpedros/patch-1TJ Holowaychukbefore adoption886d7e6

History of this file

Take it

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

Changed express.methods to include only those supported by nodeTj Holowaychukbefore adoption5e56446

lines 1–81why, one step back
1/*!
2 * Express - View3 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>4 * MIT Licensed5 */67/**8 * Module dependencies.9 */1011var path = require('path')12 , utils = require('./utils')13 , dirname = path.dirname14 , basename = path.basename15 , extname = path.extname16 , exists = path.existsSync17 , join = path.join;1819/**20 * Expose `View`.21 */2223module.exports = View;2425/**26 * Initialize a new `View` with the given `name`.27 *28 * Options:29 *30 * - `defaultEngine` the default template engine name 31 * - `engines` template engine require() cache 32 * - `root` root path for view lookup 33 *34 * @param {String} name35 * @param {Object} options36 * @api private37 */3839function View(name, options) {40 options = options || {};41 this.name = name;42 this.root = options.root;43 var engines = options.engines;44 this.defaultEngine = options.defaultEngine;45 var ext = this.ext = extname(name);46 if (!ext) name += (ext = this.ext = '.' + this.defaultEngine);47 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express);48 this.path = this.lookup(name);49}5051/**52 * Lookup view by the given `path`53 *54 * @param {String} path55 * @return {String}56 * @api private57 */5859View.prototype.lookup = function(path){60 var ext = this.ext;6162 // <path>.<engine>63 if (!utils.isAbsolute(path)) path = join(this.root, path);64 if (exists(path)) return path;6566 // <path>/index.<engine>67 path = join(dirname(path), basename(path, ext), 'index' + ext);68 if (exists(path)) return path;69};7071/**72 * Render with the given `options` and callback `fn(err, str)`.73 *74 * @param {Object} options75 * @param {Function} fn76 * @api private77 */7879View.prototype.render = function(options, fn){80 this.engine(this.path, options, fn);81};