Skip to content
Myr

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 file

Take 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 back
1
2/*!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 back
18 , partial = require('./view/partial')

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 19–36why, one step back
19 , union = require('./utils').union
20 , 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 back
37 * Lookup `view`, returning an instanceof `View`.
38 *

Updated jade submoduleTj Holowaychukbefore adoptioneeb7754

lines 39–43why, one step back
39 * 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 back
44 * @param {String} view
45 * @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 back
77 * Partial render helper.
78 *79 * @api private80 */81

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

lines 82–84why, one step back
82function renderPartial(res, view, options, parentLocals, parent){
83 var collection, object, locals;84

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 85–90why, one step back
85 // Inherit parent view extension when not present
86 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 back
91 // collection
92 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 back
98 }

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

lines 99–113why, one step back
99
100 // 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 back
114 } else {
115 options = {};116 }117118 // Inherit locals from parent

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

line 119why, one step back
119 union(options, parentLocals);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 120–121why, one step back
120
121 // Merge locals

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

line 122why, one step back
122 if (locals) merge(options, locals);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 123–128why, one step back
123
124 // Partials dont need layouts125 options.renderPartial = true;126 options.layout = false;127128 // Deduce name from view path

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

line 129why, one step back
129 var name = options.as || partial.resolveObjectName(view);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 130–132why, one step back
130
131 // Render partial132 function render(){

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

line 133why, one step back
133 if (object) {

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 134why, one step back
134 if ('string' == typeof name) {

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

line 135why, one step back
135 options[name] = object;

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 136why, one step back
136 } else if (name === global) {

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

line 137why, one step back
137 merge(options, object);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 138why, one step back
138 } else {

Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f

line 139why, one step back
139 options.scope = object;

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 140–141why, one step back
140 }
141 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 142why, one step back
142 return res.render(view, options, null, parent, true);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 143–147why, one step back
143 }
144145 // Collection support146 if (collection) {147 var len = collection.length

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 148–152why, one step back
148 , buf = ''
149 , keys150 , key151 , val;152

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 153why, one step back
153 options.collectionLength = len;

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 154–171why, one step back
154
155 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 back
172 options.keyInCollection = key;

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 173–178why, one step back
173 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 back
179 }

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

line 180why, one step back
180

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 181–187why, one step back
181 return buf;
182 } else {183 return render();184 }185};186187/**

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 188–190why, one step back
188 * 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 back
191 *
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} view

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 205–206why, one step back
205 * @param {Object|Array|Function} options, collection, callback, or object
206 * @param {Function} fn

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 207–210why, one step back
207 * @return {String}
208 * @api public209 */210

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

line 211why, one step back
211res.partial = function(view, options, fn){

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 212–215why, one step back
212 var app = this.app
213 , options = options || {}214 , parent = {};215

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 216–221why, one step back
216 // accept callback as second argument
217 if ('function' == typeof options) {218 fn = options;219 options = {};220 }221

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 222–229why, one step back
222 // root "views" option
223 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 }229

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 230–236why, one step back
230 // render the partial
231 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 back
237 this.req.next(err);

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

line 238why, one step back
238 }

Fixed res.partial(); next(err) when no callback is given [reported by aheckmann]Tj Holowaychukbefore adoption28ba9e8

line 239why, one step back
239 return;

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 240–247why, one step back
240 }
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 back
248};
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 information

docsTj Holowaychukbefore adoption2007407

line 259why, one step back
259 * - `status` Response status code

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 260–266why, one step back
260 *
261 * @param {String} view262 * @param {Object|Function} options or callback function263 * @param {Function} fn264 * @api public265 */266

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 267why, one step back
267res.render = function(view, opts, fn, parent, sub){

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 268why, one step back
268 // support callback function as second arg

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 269why, one step back
269 if ('function' == typeof opts) {

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 270–271why, one step back
270 fn = opts, opts = null;
271 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 272why, one step back
272

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 273–278why, one step back
273 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 back
279 // unwind to root call to prevent
280 // several next(err) calls

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 281–292why, one step back
281 } 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 back
293 var options = {}
294 , self = this295 , app = this.app

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

line 296why, one step back
296 , helpers = app._locals

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 297–298why, one step back
297 , dynamicHelpers = app.dynamicViewHelpers
298 , viewOptions = app.set('view options')

Renamed "cache views" to "view cache". Closes #628Tj Holowaychukbefore adoptiond9e7153

line 299why, one step back
299 , cacheViews = app.enabled('view cache')

misc refactoringTj Holowaychukbefore adoption11250d2

line 300why, one step back
300 , root = app.set('views') || process.cwd() + '/views';

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 301why, one step back
301

Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c

lines 302–304why, one step back
302 // cache id
303 var cid = view + (parent ? ':' + parent.path : '');304

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 305–307why, one step back
305 // merge "view options"
306 if (viewOptions) merge(options, viewOptions);307

Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a

lines 308–309why, one step back
308 // merge res._locals
309 if (this._locals) merge(options, this._locals);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 310–313why, one step back
310
311 // merge render() options312 if (opts) merge(options, opts);313

Added res.render() .locals support back to aid in migration processTj Holowaychukbefore adoption07d06ad

lines 314–316why, one step back
314 // merge render() .locals
315 if (opts && opts.locals) merge(options, opts.locals);316

Added res.render() status support backTj Holowaychukbefore adoptiond152e7e

lines 317–319why, one step back
317 // status support
318 if (options.status) this.statusCode = options.status;319

Fixed view hintsTj Holowaychukbefore adoptionae10789

lines 320–322why, one step back
320 // capture attempts
321 options.attempts = [];322

misc refactoringTj Holowaychukbefore adoption11250d2

line 323why, one step back
323 var partial = options.renderPartial

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 324–342why, one step back
324 , 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');342

Added charset option for render()Tj Holowaychukbefore adoptionf327455

lines 343–344why, one step back
343 // charset option
344 if (options.charset) this.charset = options.charset;

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 345–370why, one step back
345
346 // 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 };370

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

line 371why, one step back
371 // cached view

Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c

lines 372–373why, one step back
372 if (app.cache[cid]) {
373 view = app.cache[cid];

Fixed filename option passing to template engineTj Holowaychukbefore adoptionf448734

line 374why, one step back
374 options.filename = view.path;

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

lines 375–376why, one step back
375 // resolve view
376 } else {

added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672

line 377why, one step back
377 view = exports.lookup(view, options);

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

lines 378–379why, one step back
378
379 if (!view.exists) {

added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672

lines 380–382why, one step back
380 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 back
383 throw err;
384 } 385

Fixed filename option passing to template engineTj Holowaychukbefore adoptionf448734

line 386why, one step back
386 options.filename = view.path;

added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672

line 387why, one step back
387 view.fn = view.templateEngine.compile(view.contents, options);

Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c

line 388why, one step back
388 if (cacheViews) app.cache[cid] = view;

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

lines 389–390why, one step back
389 }
390

Merge branch 'feature/view-layout-control'Tj Holowaychukbefore adoption6a58c71

lines 391–395why, one step back
391 // layout helper
392 options.layout = function(path){393 layout = path;394 };395

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

lines 396–397why, one step back
396 // render
397 var str = view.fn.call(options.scope, options);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 398why, one step back
398

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 399–414why, one step back
399 // layout expected
400 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 back
415 }

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

line 416why, one step back
416}

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 417–427why, one step back
417
418/**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 back
428 options.attempts.forEach(function(path){
429 console.error(' - %s', path);430 });

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 431–432why, one step back
431 console.error();
432}