view.js
This file
Path
lib/view.js
432 lines · 10.0 KiB · JavaScript
Last changed by
Updated jade submoduleTj Holowaychukbefore adoptioneeb7754
History of this fileTake it
The walk stopped after 200 commits: lines older than 4c274c5 point at it.
Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
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 res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 19–36why, one step back19 , union = require('./utils').union20 , merge = utils.merge21 , http = require('http')22 , res = http.ServerResponse.prototype;2324/**25 * Expose constructors.26 */2728exports = module.exports = View;2930/**31 * Export template engine registrar.32 */3334exports.register = View.register;3536/**added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
lines 37–38why, one step back37 * Lookup `view`, returning an instanceof `View`.38 *Updated jade submoduleTj Holowaychukbefore adoptioneeb7754
lines 39–43why, one step back39 * Options:40 *41 * - `root` root directory path42 * - `defaultEngine` default template engine43 *added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
lines 44–76why, one step back44 * @param {String} view45 * @param {Object} options46 * @return {View}47 * @api private48 */4950exports.lookup = function(view, options){51 var orig = view = new View(view, options);5253 // Try _ prefix ex: ./views/_<name>.jade54 if (partial) {55 view = new View(orig.prefixPath, options);56 if (!view.exists) view = orig;57 }5859 // Try index ex: ./views/user/index.jade60 if (!view.exists) view = new View(orig.indexPath, options);6162 // Try ../<name>/index ex: ../user/index.jade63 // when calling partial('user') within the same dir64 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);6566 // Try root ex: <root>/user.jade67 if (!view.exists) view = new View(orig.rootPath, options);6869 // Try root _ prefix ex: <root>/_user.jade70 if (!view.exists && partial) view = new View(view.prefixPath, options);7172 view.original = orig;73 return view;74};7576/**Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 77–81why, one step back77 * Partial render helper.78 *79 * @api private80 */81Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 82–84why, one step back82function renderPartial(res, view, options, parentLocals, parent){83 var collection, object, locals;84Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 85–90why, one step back85 // Inherit parent view extension when not present86 if (parent && !~view.indexOf('.')) {87 view += parent.extension;88 }8990 if (options) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 91–97why, one step back91 // collection92 if (options.collection) {93 collection = options.collection;94 delete options.collection;95 } else if ('length' in options) {96 collection = options;97 options = {};Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 98why, one step back98 }Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 99–113why, one step back99100 // locals101 if (options.locals) {102 locals = options.locals;103 delete options.locals;104 }105106 // object107 if ('Object' != options.constructor.name) {108 object = options;109 options = {};110 } else if (undefined != options.object) {111 object = options.object;112 delete options.object;113 }Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 114–118why, one step back114 } else {115 options = {};116 }117118 // Inherit locals from parentChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 119why, one step back119 union(options, parentLocals);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 120–121why, one step back120 121 // Merge localsChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 122why, one step back122 if (locals) merge(options, locals);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 123–128why, one step back123124 // Partials dont need layouts125 options.renderPartial = true;126 options.layout = false;127128 // Deduce name from view pathRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 129why, one step back129 var name = options.as || partial.resolveObjectName(view);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 130–132why, one step back130131 // Render partial132 function render(){Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 133why, one step back133 if (object) {Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 134why, one step back134 if ('string' == typeof name) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 135why, one step back135 options[name] = object;Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 136why, one step back136 } else if (name === global) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 137why, one step back137 merge(options, object);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 138why, one step back138 } else {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 139why, one step back139 options.scope = object;Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 140–141why, one step back140 }141 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 142why, one step back142 return res.render(view, options, null, parent, true);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 143–147why, one step back143 }144145 // Collection support146 if (collection) {147 var len = collection.lengthAdded better partial() object collection supportTj Holowaychukbefore adoption08046f7
lines 148–152why, one step back148 , buf = ''149 , keys150 , key151 , val;152Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 153why, one step back153 options.collectionLength = len;Added better partial() object collection supportTj Holowaychukbefore adoption08046f7
lines 154–171why, one step back154155 if ('number' == typeof len || Array.isArray(collection)) {156 for (var i = 0; i < len; ++i) {157 val = collection[i];158 options.firstInCollection = i == 0;159 options.indexInCollection = i;160 options.lastInCollection = i == len - 1;161 object = val;162 buf += render();163 } 164 } else {165 keys = Object.keys(collection);166 len = keys.length;167 options.collectionLength = len;168 options.collectionKeys = keys;169 for (var i = 0; i < len; ++i) {170 key = keys[i];171 val = collection[key];misc refactoringTj Holowaychukbefore adoption89383ec
line 172why, one step back172 options.keyInCollection = key;Added better partial() object collection supportTj Holowaychukbefore adoption08046f7
lines 173–178why, one step back173 options.firstInCollection = i == 0;174 options.indexInCollection = i;175 options.lastInCollection = i == len - 1;176 object = val;177 buf += render();178 }Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 179why, one step back179 }Added better partial() object collection supportTj Holowaychukbefore adoption08046f7
line 180why, one step back180Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 181–187why, one step back181 return buf;182 } else {183 return render();184 }185};186187/**Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 188–190why, one step back188 * Render `view` partial with the given `options`. Optionally a 189 * callback `fn(err, str)` may be passed instead of writing to190 * the socket.Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 191–204why, one step back191 *192 * Options:193 *194 * - `object` Single object with name derived from the view (unless `as` is present) 195 *196 * - `as` Variable name for each `collection` value, defaults to the view name.197 * * as: 'something' will add the `something` local variable198 * * as: this will use the collection value as the template context199 * * as: global will merge the collection value's properties with `locals`200 *201 * - `collection` Array of objects, the name is derived from the view name itself. 202 * For example _video.html_ will have a object _video_ available to it.203 *204 * @param {String} viewAdded res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 205–206why, one step back205 * @param {Object|Array|Function} options, collection, callback, or object206 * @param {Function} fnAdded res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 207–210why, one step back207 * @return {String}208 * @api public209 */210Added res.partial() callback supportTj Holowaychukbefore adoption723c908
line 211why, one step back211res.partial = function(view, options, fn){Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 212–215why, one step back212 var app = this.app213 , options = options || {}214 , parent = {};215Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 216–221why, one step back216 // accept callback as second argument217 if ('function' == typeof options) {218 fn = options;219 options = {};220 }221Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 222–229why, one step back222 // root "views" option223 parent.dirname = app.set('views') || process.cwd() + '/views';224225 // utilize "view engine" option226 if (app.set('view engine')) {227 parent.extension = '.' + app.set('view engine');228 }229Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 230–236why, one step back230 // render the partial231 try {232 var str = renderPartial(this, view, options, null, parent);233 } catch (err) {234 if (fn) {235 fn(err);236 } else {Fixed res.partial(); next(err) when no callback is given [reported by aheckmann]Tj Holowaychukbefore adoption28ba9e8
line 237why, one step back237 this.req.next(err);Added res.partial() callback supportTj Holowaychukbefore adoption723c908
line 238why, one step back238 }Fixed res.partial(); next(err) when no callback is given [reported by aheckmann]Tj Holowaychukbefore adoption28ba9e8
line 239why, one step back239 return;Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 240–247why, one step back240 }241242 // callback or transfer243 if (fn) {244 fn(null, str);245 } else {246 this.send(str);247 }Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 248–258why, one step back248};249250/**251 * Render `view` with the given `options` and optional callback `fn`.252 * When a callback function is given a response will _not_ be made253 * automatically, however otherwise a response of _200_ and _text/html_ is given.254 *255 * Options:256 * 257 * - `scope` Template evaluation context (the value of `this`)258 * - `debug` Output debugging informationdocsTj Holowaychukbefore adoption2007407
line 259why, one step back259 * - `status` Response status codeAdded res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 260–266why, one step back260 *261 * @param {String} view262 * @param {Object|Function} options or callback function263 * @param {Function} fn264 * @api public265 */266refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 267why, one step back267res.render = function(view, opts, fn, parent, sub){Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 268why, one step back268 // support callback function as second argrefactored res.render()Tj Holowaychukbefore adoption2ff991b
line 269why, one step back269 if ('function' == typeof opts) {Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 270–271why, one step back270 fn = opts, opts = null;271 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 272why, one step back272wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 273–278why, one step back273 try {274 return this._render(view, opts, fn, parent, sub);275 } catch (err) {276 // callback given277 if (fn) {278 fn(err);docsTj Holowaychukbefore adoption7888cb0
lines 279–280why, one step back279 // unwind to root call to prevent280 // several next(err) callswrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 281–292why, one step back281 } else if (sub) {282 throw err;283 // root template, next(err)284 } else {285 this.req.next(err);286 }287 }288};289290// private render()291292res._render = function(view, opts, fn, parent, sub){Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 293–295why, one step back293 var options = {}294 , self = this295 , app = this.appviewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 296why, one step back296 , helpers = app._localsAdded res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 297–298why, one step back297 , dynamicHelpers = app.dynamicViewHelpers298 , viewOptions = app.set('view options')Renamed "cache views" to "view cache". Closes #628Tj Holowaychukbefore adoptiond9e7153
line 299why, one step back299 , cacheViews = app.enabled('view cache')misc refactoringTj Holowaychukbefore adoption11250d2
line 300why, one step back300 , root = app.set('views') || process.cwd() + '/views';Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 301why, one step back301Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c
lines 302–304why, one step back302 // cache id303 var cid = view + (parent ? ':' + parent.path : '');304Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 305–307why, one step back305 // merge "view options"306 if (viewOptions) merge(options, viewOptions);307Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a
lines 308–309why, one step back308 // merge res._locals309 if (this._locals) merge(options, this._locals);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 310–313why, one step back310311 // merge render() options312 if (opts) merge(options, opts);313Added res.render() .locals support back to aid in migration processTj Holowaychukbefore adoption07d06ad
lines 314–316why, one step back314 // merge render() .locals315 if (opts && opts.locals) merge(options, opts.locals);316Added res.render() status support backTj Holowaychukbefore adoptiond152e7e
lines 317–319why, one step back317 // status support318 if (options.status) this.statusCode = options.status;319Fixed view hintsTj Holowaychukbefore adoptionae10789
lines 320–322why, one step back320 // capture attempts321 options.attempts = [];322misc refactoringTj Holowaychukbefore adoption11250d2
line 323why, one step back323 var partial = options.renderPartialAdded res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 324–342why, one step back324 , layout = options.layout;325326 // Layout support327 if (true === layout || undefined === layout) {328 layout = 'layout';329 }330331 // Default execution scope to a plain object332 options.scope = options.scope || {};333334 // Populate view335 options.parentView = parent;336337 // "views" setting338 options.root = root;339340 // "view engine" setting341 options.defaultEngine = app.set('view engine');342Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 343–344why, one step back343 // charset option344 if (options.charset) this.charset = options.charset;Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
lines 345–370why, one step back345346 // Dynamic helper support347 if (false !== options.dynamicHelpers) {348 // cache349 if (!this.__dynamicHelpers) {350 this.__dynamicHelpers = {};351 for (var key in dynamicHelpers) {352 this.__dynamicHelpers[key] = dynamicHelpers[key].call(353 this.app354 , this.req355 , this);356 }357 }358359 // apply360 merge(options, this.__dynamicHelpers);361 }362363 // Merge view helpers364 union(options, helpers);365366 // Always expose partial() as a local367 options.partial = function(path, opts){368 return renderPartial(self, path, opts, options, view);369 };370Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
line 371why, one step back371 // cached viewFixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c
lines 372–373why, one step back372 if (app.cache[cid]) {373 view = app.cache[cid];Fixed filename option passing to template engineTj Holowaychukbefore adoptionf448734
line 374why, one step back374 options.filename = view.path;Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 375–376why, one step back375 // resolve view376 } else {added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
line 377why, one step back377 view = exports.lookup(view, options);Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 378–379why, one step back378379 if (!view.exists) {added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
lines 380–382why, one step back380 if (app.enabled('hints')) hintAtViewPaths(view.original, options);381 var err = new Error('failed to locate view "' + view.original.view + '"');382 err.view = view.original;Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 383–385why, one step back383 throw err;384 } 385Fixed filename option passing to template engineTj Holowaychukbefore adoptionf448734
line 386why, one step back386 options.filename = view.path;added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
line 387why, one step back387 view.fn = view.templateEngine.compile(view.contents, options);Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c
line 388why, one step back388 if (cacheViews) app.cache[cid] = view;Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 389–390why, one step back389 }390Merge branch 'feature/view-layout-control'Tj Holowaychukbefore adoption6a58c71
lines 391–395why, one step back391 // layout helper392 options.layout = function(path){393 layout = path;394 };395Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 396–397why, one step back396 // render397 var str = view.fn.call(options.scope, options);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 398why, one step back398wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 399–414why, one step back399 // layout expected400 if (layout) {401 options.isLayout = true;402 options.layout = false;403 options.body = str;404 this.render(layout, options, fn, view, true);405 // partial return406 } else if (partial) {407 return str;408 // render complete, and 409 // callback given410 } else if (fn) {411 fn(null, str);412 // respond413 } else {414 this.send(str);Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5
line 415why, one step back415 }wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
line 416why, one step back416}hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 417–427why, one step back417418/**419 * Hint at view path resolution, outputting the420 * paths that Express has tried.421 *422 * @api private423 */424425function hintAtViewPaths(view, options) {426 console.error();427 console.error('failed to locate view "' + view.view + '", tried:');Fixed view hintsTj Holowaychukbefore adoptionae10789
lines 428–430why, one step back428 options.attempts.forEach(function(path){429 console.error(' - %s', path);430 });hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 431–432why, one step back431 console.error();432}