view.js
This file
Path
lib/view.js
95 lines · 1.9 KiB · JavaScript
Last changed by
map register functionTj Holowaychukbefore adoption02f48aa
History of this fileTake it
new ViewTj Holowaychukbefore adoptione1da383
lines 1–13why, one step back12/*!3 * Express - View4 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>5 * MIT Licensed6 */78/**9 * Module dependencies.10 */1112var path = require('path')13 , fs = require('fs')added index lookup support backTj Holowaychukbefore adoption041974e
lines 14–15why, one step back14 , dirname = path.dirname15 , basename = path.basenamenew ViewTj Holowaychukbefore adoptione1da383
line 16why, one step back16 , extname = path.extnameadded index lookup support backTj Holowaychukbefore adoption041974e
line 17why, one step back17 , exists = path.existsSyncnew ViewTj Holowaychukbefore adoptione1da383
lines 18–28why, one step back18 , join = path.join;1920/**21 * Expose `View`.22 */2324module.exports = View;2526/**27 * Initialize a new `View` with the given `name`.28 *docsTj Holowaychukbefore adoption7369d86
lines 29–34why, one step back29 * Options:30 *31 * - `defaultEngine` the default template engine name 32 * - `engines` template engine require() cache 33 * - `root` root path for view lookup 34 *new ViewTj Holowaychukbefore adoptione1da383
lines 35–36why, one step back35 * @param {String} name36 * @param {Object} optionsdocsTj Holowaychukbefore adoption7369d86
line 37why, one step back37 * @api privatenew ViewTj Holowaychukbefore adoptione1da383
lines 38–42why, one step back38 */3940function View(name, options) {41 options = options || {};42 this.name = name;added index lookup support backTj Holowaychukbefore adoption041974e
line 43why, one step back43 this.root = options.root;new ViewTj Holowaychukbefore adoptione1da383
lines 44–45why, one step back44 var engines = options.engines;45 this.defaultEngine = options.defaultEngine;misc refactoringTj Holowaychukbefore adoption705cdb6
line 46why, one step back46 var ext = this.ext = extname(name);map register functionTj Holowaychukbefore adoption02f48aa
lines 47–48why, one step back47 if (!ext) name += (ext = this.ext = '.' + this.defaultEngine);48 this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).render);added index lookup support backTj Holowaychukbefore adoption041974e
line 49why, one step back49 this.path = this.lookup(name);View#contents()Tj Holowaychukbefore adoption6d47011
line 50why, one step back50 this.string = this.contents();new ViewTj Holowaychukbefore adoptione1da383
line 51why, one step back51}View#contents()Tj Holowaychukbefore adoption6d47011
lines 52–62why, one step back5253/**54 * Return the contents of this view.55 *56 * @return {String}57 * @api private58 */5960View.prototype.contents = function(){61 return fs.readFileSync(this.path, 'utf8');62};added index lookup support backTj Holowaychukbefore adoption041974e
line 63why, one step back63docsTj Holowaychukbefore adoption90fc80f
lines 64–72why, one step back64/**65 * Lookup view by the given `path`66 *67 * @param {String} path68 * @return {String}69 * @api private70 */7172View.prototype.lookup = function(path){map register functionTj Holowaychukbefore adoption02f48aa
line 73why, one step back73 var ext = this.ext;more refactoringTj Holowaychukbefore adoption371b381
line 74why, one step back74added index lookup support backTj Holowaychukbefore adoption041974e
line 75why, one step back75 // <path>.<engine>docsTj Holowaychukbefore adoption90fc80f
line 76why, one step back76 path = join(this.root, path);added index lookup support backTj Holowaychukbefore adoption041974e
lines 77–79why, one step back77 if (exists(path)) return path;7879 // <path>/index.<engine>more refactoringTj Holowaychukbefore adoption371b381
line 80why, one step back80 path = join(dirname(path), basename(path, ext), 'index' + ext);added index lookup support backTj Holowaychukbefore adoption041974e
lines 81–82why, one step back81 if (exists(path)) return path;82};docsTj Holowaychukbefore adoption7369d86
lines 83–90why, one step back8384/**85 * Render with the given `options` and callback `fn(err, str)`.86 *87 * @param {Object} options88 * @param {Function} fn89 * @api private90 */new ViewTj Holowaychukbefore adoptione1da383
lines 91–93why, one step back9192View.prototype.render = function(options, fn){93 options.filename = this.path;map register functionTj Holowaychukbefore adoption02f48aa
line 94why, one step back94 this.engine(this.string, options, fn);new ViewTj Holowaychukbefore adoptione1da383
line 95why, one step back95};