view.js
This file
Path
lib/view.js
377 lines · 9.0 KiB · JavaScript
Last changed by
wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
History of this fileTake it
The walk stopped after 200 commits: lines older than e958988 point at it.
styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 1–17why, 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 , extname = path.extname14 , dirname = path.dirname15 , basename = path.basename16 , utils = require('connect').utils17 , View = require('./view/view')Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 18why, one step back18 , partial = require('./view/partial')Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 19why, one step back19 , union = require('./utils').unionstyling for stylus exampleTj Holowaychukbefore adoptione958988
line 20why, one step back20 , merge = utils.mergeAdded res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 21–22why, one step back21 , http = require('http')22 , res = http.ServerResponse.prototype;styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 23–44why, one step back2324/**25 * Memory cache.26 *27 * @type Object28 */2930var cache = {};3132/**33 * Expose constructors.34 */3536exports = module.exports = View;3738/**39 * Export template engine registrar.40 */4142exports.register = View.register;4344/**Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 45why, one step back45 * Partial render helper.styling for stylus exampleTj Holowaychukbefore adoptione958988
line 46why, one step back46 *Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 47why, one step back47 * @api privatestyling for stylus exampleTj Holowaychukbefore adoptione958988
lines 48–49why, one step back48 */49Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 50–52why, one step back50function renderPartial(res, view, options, parentLocals, parent){51 var collection, object, locals;52styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 53–58why, one step back53 // Inherit parent view extension when not present54 if (parent && !~view.indexOf('.')) {55 view += parent.extension;56 }5758 if (options) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 59–65why, one step back59 // collection60 if (options.collection) {61 collection = options.collection;62 delete options.collection;63 } else if ('length' in options) {64 collection = options;65 options = {};styling for stylus exampleTj Holowaychukbefore adoptione958988
line 66why, one step back66 }Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 67–81why, one step back6768 // locals69 if (options.locals) {70 locals = options.locals;71 delete options.locals;72 }7374 // object75 if ('Object' != options.constructor.name) {76 object = options;77 options = {};78 } else if (undefined != options.object) {79 object = options.object;80 delete options.object;81 }styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 82–86why, one step back82 } else {83 options = {};84 }8586 // Inherit locals from parentChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 87why, one step back87 union(options, parentLocals);styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 88–89why, one step back88 89 // Merge localsChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 90why, one step back90 if (locals) merge(options, locals);styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 91–96why, one step back9192 // Partials dont need layouts93 options.renderPartial = true;94 options.layout = false;9596 // Deduce name from view pathRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 97why, one step back97 var name = options.as || partial.resolveObjectName(view);styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 98–100why, one step back9899 // Render partial100 function render(){Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 101why, one step back101 if (object) {styling for stylus exampleTj Holowaychukbefore adoptione958988
line 102why, one step back102 if ('string' == typeof name) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 103why, one step back103 options[name] = object;styling for stylus exampleTj Holowaychukbefore adoptione958988
line 104why, one step back104 } else if (name === global) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 105why, one step back105 merge(options, object);styling for stylus exampleTj Holowaychukbefore adoptione958988
line 106why, one step back106 } else {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 107why, one step back107 options.scope = object;styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 108–109why, one step back108 }109 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 110why, one step back110 return res.render(view, options, null, parent, true);styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 111–122why, one step back111 }112113 // Collection support114 if (collection) {115 var len = collection.length116 , buf = '';117 options.collectionLength = len;118 for (var i = 0; i < len; ++i) {119 var val = collection[i];120 options.firstInCollection = i === 0;121 options.indexInCollection = i;122 options.lastInCollection = i === len - 1;Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 123why, one step back123 object = val;styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 124–132why, one step back124 buf += render();125 }126 return buf;127 } else {128 return render();129 }130};131132/**Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 133–135why, one step back133 * Render `view` partial with the given `options`. Optionally a 134 * callback `fn(err, str)` may be passed instead of writing to135 * the socket.Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 136–149why, one step back136 *137 * Options:138 *139 * - `object` Single object with name derived from the view (unless `as` is present) 140 *141 * - `as` Variable name for each `collection` value, defaults to the view name.142 * * as: 'something' will add the `something` local variable143 * * as: this will use the collection value as the template context144 * * as: global will merge the collection value's properties with `locals`145 *146 * - `collection` Array of objects, the name is derived from the view name itself. 147 * For example _video.html_ will have a object _video_ available to it.148 *149 * @param {String} viewAdded res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 150–151why, one step back150 * @param {Object|Array|Function} options, collection, callback, or object151 * @param {Function} fnAdded res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 152–155why, one step back152 * @return {String}153 * @api public154 */155Added res.partial() callback supportTj Holowaychukbefore adoption723c908
line 156why, one step back156res.partial = function(view, options, fn){Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 157–160why, one step back157 var app = this.app158 , options = options || {}159 , parent = {};160Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 161–166why, one step back161 // accept callback as second argument162 if ('function' == typeof options) {163 fn = options;164 options = {};165 }166Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 167–174why, one step back167 // root "views" option168 parent.dirname = app.set('views') || process.cwd() + '/views';169170 // utilize "view engine" option171 if (app.set('view engine')) {172 parent.extension = '.' + app.set('view engine');173 }174Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 175–191why, one step back175 // render the partial176 try {177 var str = renderPartial(this, view, options, null, parent);178 } catch (err) {179 if (fn) {180 fn(err);181 } else {182 throw err;183 }184 }185186 // callback or transfer187 if (fn) {188 fn(null, str);189 } else {190 this.send(str);191 }Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 192–194why, one step back192};193194/**styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 195–202why, one step back195 * Render `view` with the given `options` and optional callback `fn`.196 * When a callback function is given a response will _not_ be made197 * automatically, however otherwise a response of _200_ and _text/html_ is given.198 *199 * Options:200 * 201 * - `scope` Template evaluation context (the value of `this`)202 * - `debug` Output debugging informationdocsTj Holowaychukbefore adoption2007407
line 203why, one step back203 * - `status` Response status codestyling for stylus exampleTj Holowaychukbefore adoptione958988
lines 204–210why, one step back204 *205 * @param {String} view206 * @param {Object|Function} options or callback function207 * @param {Function} fn208 * @api public209 */210refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 211why, one step back211res.render = function(view, opts, fn, parent, sub){Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 212why, one step back212 // support callback function as second argrefactored res.render()Tj Holowaychukbefore adoption2ff991b
line 213why, one step back213 if ('function' == typeof opts) {Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 214why, one step back214 fn = opts, opts = null;styling for stylus exampleTj Holowaychukbefore adoptione958988
line 215why, one step back215 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 216why, one step back216wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 217–235why, one step back217 try {218 return this._render(view, opts, fn, parent, sub);219 } catch (err) {220 // callback given221 if (fn) {222 fn(err);223 // unwind to root call224 } else if (sub) {225 throw err;226 // root template, next(err)227 } else {228 this.req.next(err);229 }230 }231};232233// private render()234235res._render = function(view, opts, fn, parent, sub){Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 236–237why, one step back236 var options = {}237 , self = thisstyling for stylus exampleTj Holowaychukbefore adoptione958988
lines 238–240why, one step back238 , app = this.app239 , helpers = app.viewHelpers240 , dynamicHelpers = app.dynamicViewHelpersMerge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 241–242why, one step back241 , viewOptions = app.set('view options')242 , cacheTemplates = app.set('cache views');styling for stylus exampleTj Holowaychukbefore adoptione958988
line 243why, one step back243Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 244why, one step back244 // merge "view options"misc refactoringTj Holowaychukbefore adoption530328a
line 245why, one step back245 if (viewOptions) merge(options, viewOptions);styling for stylus exampleTj Holowaychukbefore adoptione958988
line 246why, one step back246Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a
lines 247–248why, one step back247 // merge res._locals248 if (this._locals) merge(options, this._locals);Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 249–250why, one step back249250 // merge render() optionsmisc refactoringTj Holowaychukbefore adoption530328a
line 251why, one step back251 if (opts) merge(options, opts);Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 252why, one step back252Added res.render() .locals support back to aid in migration processTj Holowaychukbefore adoption07d06ad
lines 253–255why, one step back253 // merge render() .locals254 if (opts && opts.locals) merge(options, opts.locals);255Added res.render() status support backTj Holowaychukbefore adoptiond152e7e
lines 256–258why, one step back256 // status support257 if (options.status) this.statusCode = options.status;258styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 259–260why, one step back259 // Defaults260 var self = thisutilizing app.set()Tj Holowaychukbefore adoption334604d
line 261why, one step back261 , root = app.set('views') || process.cwd() + '/views'styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 262–269why, one step back262 , partial = options.renderPartial263 , layout = options.layout;264265 // Layout support266 if (true === layout || undefined === layout) {267 layout = 'layout';268 }269Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4
lines 270–271why, one step back270 // Default execution scope to a plain object271 options.scope = options.scope || {};styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 272–279why, one step back272273 // Populate view274 options.parentView = parent;275276 // "views" setting277 options.root = root;278279 // "view engine" settingutilizing app.set()Tj Holowaychukbefore adoption334604d
line 280why, one step back280 options.defaultEngine = app.set('view engine');styling for stylus exampleTj Holowaychukbefore adoptione958988
line 281why, one step back281Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 282–284why, one step back282 // charset option283 if (options.charset) this.charset = options.charset;284styling for stylus exampleTj Holowaychukbefore adoptione958988
line 285why, one step back285 // Populate viewRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 286why, one step back286 var orig = view = new View(view, options);styling for stylus exampleTj Holowaychukbefore adoptione958988
line 287why, one step back287Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
lines 288–289why, one step back288 // Try _ prefix ex: ./views/_user.jade289 if (!view.exists) view = new View(orig.prefixPath, options);styling for stylus exampleTj Holowaychukbefore adoptione958988
line 290why, one step back290 Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
line 291why, one step back291 // Try index ex: ./views/user/index.jademisc refactorTj Holowaychukbefore adoption110b0fe
line 292why, one step back292 if (!view.exists) view = new View(orig.indexPath, options);Added View#upIndexPathTj Holowaychukbefore adoption32be4a7
line 293why, one step back293Fixed up index view path alternativeTj Holowaychukbefore adoption220d88d
lines 294–295why, one step back294 // Try ../<name>/index ex: ../user/index.jade295 // when calling partial('user') within the same dirmisc refactorTj Holowaychukbefore adoption110b0fe
line 296why, one step back296 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);fixing testsTj Holowaychukbefore adoption2b907e9
line 297why, one step back297Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6
lines 298–299why, one step back298 // Try layout relative to the "views" dir 299 if (!view.exists && options.isLayout) view = new View(orig.rootPath, options);Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
lines 300–302why, one step back300301 // Does not exist302 if (!view.exists) {hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 303–304why, one step back303 if (app.enabled('hints')) hintAtViewPaths(orig, options);304 throw new Error('failed to locate view "' + orig.view + '"');Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
line 305why, one step back305 }Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6
line 306why, one step back306styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 307–324why, one step back307 // Dynamic helper support308 if (false !== options.dynamicHelpers) {309 // cache310 if (!this.__dynamicHelpers) {311 this.__dynamicHelpers = {};312 for (var key in dynamicHelpers) {313 this.__dynamicHelpers[key] = dynamicHelpers[key].call(314 this.app315 , this.req316 , this);317 }318 }319320 // apply321 merge(options, this.__dynamicHelpers);322 }323324 // Merge view helpersAdded union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 325why, one step back325 union(options, helpers);styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 326–328why, one step back326327 // Always expose partial() as a local328 options.partial = function(path, opts){Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 329why, one step back329 return renderPartial(self, path, opts, options, view);styling for stylus exampleTj Holowaychukbefore adoptione958988
lines 330–331why, one step back330 };331Fixed .filename view engine option [reported by drudge]Tj Holowaychukbefore adoptiona6fdc1b
lines 332–334why, one step back332 // Provide filename to engine333 options.filename = view.path;334styling for stylus exampleTj Holowaychukbefore adoptione958988
line 335why, one step back335 // Attempt renderwrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 336–340why, one step back336 var engine = view.templateEngine337 , template = cacheTemplates338 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))339 : engine.compile(view.contents, options)340 , str = template.call(options.scope, options);styling for stylus exampleTj Holowaychukbefore adoptione958988
line 341why, one step back341wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 342–357why, one step back342 // layout expected343 if (layout) {344 options.isLayout = true;345 options.layout = false;346 options.body = str;347 this.render(layout, options, fn, view, true);348 // partial return349 } else if (partial) {350 return str;351 // render complete, and 352 // callback given353 } else if (fn) {354 fn(null, str);355 // respond356 } else {357 this.send(str);styling for stylus exampleTj Holowaychukbefore adoptione958988
line 358why, one step back358 }wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
line 359why, one step back359}hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 360–377why, one step back360361/**362 * Hint at view path resolution, outputting the363 * paths that Express has tried.364 *365 * @api private366 */367368function hintAtViewPaths(view, options) {369 console.error();370 console.error('failed to locate view "' + view.view + '", tried:');371 console.error(' - ' + new View(view.path, options).path);372 console.error(' - ' + new View(view.prefixPath, options).path);373 console.error(' - ' + new View(view.indexPath, options).path);374 if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);375 if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);376 console.error();377}