rc@73: /** rc@73: * Module dependencies. rc@73: */ rc@73: rc@73: var path = require('path'); rc@73: var fs = require('fs'); rc@73: var utils = require('./utils'); rc@73: var dirname = path.dirname; rc@73: var basename = path.basename; rc@73: var extname = path.extname; rc@73: var exists = fs.existsSync || path.existsSync; rc@73: var join = path.join; rc@73: rc@73: /** rc@73: * Expose `View`. rc@73: */ rc@73: rc@73: module.exports = View; rc@73: rc@73: /** rc@73: * Initialize a new `View` with the given `name`. rc@73: * rc@73: * Options: rc@73: * rc@73: * - `defaultEngine` the default template engine name rc@73: * - `engines` template engine require() cache rc@73: * - `root` root path for view lookup rc@73: * rc@73: * @param {String} name rc@73: * @param {Object} options rc@73: * @api private rc@73: */ rc@73: rc@73: function View(name, options) { rc@73: options = options || {}; rc@73: this.name = name; rc@73: this.root = options.root; rc@73: var engines = options.engines; rc@73: this.defaultEngine = options.defaultEngine; rc@73: var ext = this.ext = extname(name); rc@73: if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.'); rc@73: if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine); rc@73: this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express); rc@73: this.path = this.lookup(name); rc@73: } rc@73: rc@73: /** rc@73: * Lookup view by the given `path` rc@73: * rc@73: * @param {String} path rc@73: * @return {String} rc@73: * @api private rc@73: */ rc@73: rc@73: View.prototype.lookup = function(path){ rc@73: var ext = this.ext; rc@73: rc@73: // . rc@73: if (!utils.isAbsolute(path)) path = join(this.root, path); rc@73: if (exists(path)) return path; rc@73: rc@73: // /index. rc@73: path = join(dirname(path), basename(path, ext), 'index' + ext); rc@73: if (exists(path)) return path; rc@73: }; rc@73: rc@73: /** rc@73: * Render with the given `options` and callback `fn(err, str)`. rc@73: * rc@73: * @param {Object} options rc@73: * @param {Function} fn rc@73: * @api private rc@73: */ rc@73: rc@73: View.prototype.render = function(options, fn){ rc@73: this.engine(this.path, options, fn); rc@73: };