Skip to content
Myr

view.js

This file

Path

lib/view.js

461 lines · 10.6 KiB · JavaScript

Last changed by

misc refactor of res.partial()Tj Holowaychukbefore adoption72384b0

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/**

Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e

lines 37–70why, one step back
37 * Lookup and compile `view` with cache support by supplying
38 * both the `cache` object and `cid` string,39 * followed by `options` passed to `exports.lookup()`.40 *41 * @param {String} view42 * @param {Object} cache43 * @param {Object} cid44 * @param {Object} options45 * @return {View}46 * @api private47 */4849exports.compile = function(view, cache, cid, options){50 if (cache && cid && cache[cid]) return cache[cid];5152 // lookup53 view = exports.lookup(view, options);5455 // hints56 if (!view.exists) {57 if (options.hint) hintAtViewPaths(view.original, options);58 var err = new Error('failed to locate view "' + view.original.view + '"');59 err.view = view.original;60 throw err;61 } 6263 // compile64 view.fn = view.templateEngine.compile(view.contents, options);65 cache[cid] = view;66 67 return view;68};6970/**

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

lines 71–72why, one step back
71 * Lookup `view`, returning an instanceof `View`.
72 *

Updated jade submoduleTj Holowaychukbefore adoptioneeb7754

lines 73–76why, one step back
73 * Options:
74 *75 * - `root` root directory path76 * - `defaultEngine` default template engine

docsTj Holowaychukbefore adoption8636dee

line 77why, one step back
77 * - `parentView` parent `View` object

Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e

lines 78–79why, one step back
78 * - `cache` cache object
79 * - `cacheid` optional cache id

Updated jade submoduleTj Holowaychukbefore adoptioneeb7754

line 80why, one step back
80 *

lookup docsTj Holowaychukbefore adoption1b199b7

lines 81–88why, one step back
81 * Lookup:
82 *83 * - partial `_<name>` 84 * - any `<name>/index` 85 * - non-layout `../<name>/index` 86 * - any `<root>/<name>` 87 * - partial `<root>/_<name>` 88 *

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

lines 89–121why, one step back
89 * @param {String} view
90 * @param {Object} options91 * @return {View}92 * @api private93 */9495exports.lookup = function(view, options){96 var orig = view = new View(view, options);9798 // Try _ prefix ex: ./views/_<name>.jade99 if (partial) {100 view = new View(orig.prefixPath, options);101 if (!view.exists) view = orig;102 }103104 // Try index ex: ./views/user/index.jade105 if (!view.exists) view = new View(orig.indexPath, options);106107 // Try ../<name>/index ex: ../user/index.jade108 // when calling partial('user') within the same dir109 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);110111 // Try root ex: <root>/user.jade112 if (!view.exists) view = new View(orig.rootPath, options);113114 // Try root _ prefix ex: <root>/_user.jade115 if (!view.exists && partial) view = new View(view.prefixPath, options);116117 view.original = orig;118 return view;119};120121/**

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 122–126why, one step back
122 * Partial render helper.
123 *124 * @api private125 */126

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

lines 127–129why, one step back
127function renderPartial(res, view, options, parentLocals, parent){
128 var collection, object, locals;129

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 130–135why, one step back
130 // Inherit parent view extension when not present
131 if (parent && !~view.indexOf('.')) {132 view += parent.extension;133 }134135 if (options) {

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

lines 136–142why, one step back
136 // collection
137 if (options.collection) {138 collection = options.collection;139 delete options.collection;140 } else if ('length' in options) {141 collection = options;142 options = {};

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 143why, one step back
143 }

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

lines 144–158why, one step back
144
145 // locals146 if (options.locals) {147 locals = options.locals;148 delete options.locals;149 }150151 // object152 if ('Object' != options.constructor.name) {153 object = options;154 options = {};155 } else if (undefined != options.object) {156 object = options.object;157 delete options.object;158 }

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 159–163why, one step back
159 } else {
160 options = {};161 }162163 // Inherit locals from parent

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 165–166why, one step back
165
166 // Merge locals

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 168–173why, one step back
168
169 // Partials dont need layouts170 options.renderPartial = true;171 options.layout = false;172173 // Deduce name from view path

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 175–177why, one step back
175
176 // Render partial177 function render(){

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

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

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

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

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 183why, one step back
183 } else {

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 185–186why, one step back
185 }
186 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 188–192why, one step back
188 }
189190 // Collection support191 if (collection) {192 var len = collection.length

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 193–197why, one step back
193 , buf = ''
194 , keys195 , key196 , val;197

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

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

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 199–216why, one step back
199
200 if ('number' == typeof len || Array.isArray(collection)) {201 for (var i = 0; i < len; ++i) {202 val = collection[i];203 options.firstInCollection = i == 0;204 options.indexInCollection = i;205 options.lastInCollection = i == len - 1;206 object = val;207 buf += render();208 } 209 } else {210 keys = Object.keys(collection);211 len = keys.length;212 options.collectionLength = len;213 options.collectionKeys = keys;214 for (var i = 0; i < len; ++i) {215 key = keys[i];216 val = collection[key];

misc refactoringTj Holowaychukbefore adoption89383ec

line 217why, one step back
217 options.keyInCollection = key;

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 218–223why, one step back
218 options.firstInCollection = i == 0;
219 options.indexInCollection = i;220 options.lastInCollection = i == len - 1;221 object = val;222 buf += render();223 }

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 224why, one step back
224 }

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

line 225why, one step back
225

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 226–232why, one step back
226 return buf;
227 } else {228 return render();229 }230};231232/**

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 233–235why, one step back
233 * Render `view` partial with the given `options`. Optionally a
234 * callback `fn(err, str)` may be passed instead of writing to235 * the socket.

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 236–249why, one step back
236 *
237 * Options:238 *239 * - `object` Single object with name derived from the view (unless `as` is present) 240 *241 * - `as` Variable name for each `collection` value, defaults to the view name.242 * * as: 'something' will add the `something` local variable243 * * as: this will use the collection value as the template context244 * * as: global will merge the collection value's properties with `locals`245 *246 * - `collection` Array of objects, the name is derived from the view name itself. 247 * For example _video.html_ will have a object _video_ available to it.248 *249 * @param {String} view

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 252–255why, one step back
252 * @return {String}
253 * @api public254 */255

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 257–258why, one step back
257 var app = this.app
258 , options = options || {}

misc refactor of res.partial()Tj Holowaychukbefore adoption72384b0

line 259why, one step back
259 , viewEngine = app.set('view engine')

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 260–261why, one step back
260 , parent = {};
261

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 262–267why, one step back
262 // accept callback as second argument
263 if ('function' == typeof options) {264 fn = options;265 options = {};266 }267

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 268–271why, one step back
268 // root "views" option
269 parent.dirname = app.set('views') || process.cwd() + '/views';270271 // utilize "view engine" option

misc refactor of res.partial()Tj Holowaychukbefore adoption72384b0

line 272why, one step back
272 if (viewEngine) parent.extension = '.' + viewEngine;

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 273why, one step back
273

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 274–280why, one step back
274 // render the partial
275 try {276 var str = renderPartial(this, view, options, null, parent);277 } catch (err) {278 if (fn) {279 fn(err);280 } else {

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

line 281why, one step back
281 this.req.next(err);

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

line 282why, one step back
282 }

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

line 283why, one step back
283 return;

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 284–291why, one step back
284 }
285286 // callback or transfer287 if (fn) {288 fn(null, str);289 } else {290 this.send(str);291 }

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 292–302why, one step back
292};
293294/**295 * Render `view` with the given `options` and optional callback `fn`.296 * When a callback function is given a response will _not_ be made297 * automatically, however otherwise a response of _200_ and _text/html_ is given.298 *299 * Options:300 * 301 * - `scope` Template evaluation context (the value of `this`)302 * - `debug` Output debugging information

docsTj Holowaychukbefore adoption2007407

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 304–310why, one step back
304 *
305 * @param {String} view306 * @param {Object|Function} options or callback function307 * @param {Function} fn308 * @api public309 */310

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

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

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 314–315why, one step back
314 fn = opts, opts = null;
315 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 316why, one step back
316

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 317–322why, one step back
317 try {
318 return this._render(view, opts, fn, parent, sub);319 } catch (err) {320 // callback given321 if (fn) {322 fn(err);

docsTj Holowaychukbefore adoption7888cb0

lines 323–324why, one step back
323 // unwind to root call to prevent
324 // several next(err) calls

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 325–336why, one step back
325 } else if (sub) {
326 throw err;327 // root template, next(err)328 } else {329 this.req.next(err);330 }331 }332};333334// private render()335336res._render = function(view, opts, fn, parent, sub){

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 337–339why, one step back
337 var options = {}
338 , self = this339 , app = this.app

viewHelpers -> _localsTj Holowaychukbefore adoptioneac6665

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

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

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

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

misc refactoringTj Holowaychukbefore adoption11250d2

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 345why, one step back
345

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

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 349–351why, one step back
349 // merge "view options"
350 if (viewOptions) merge(options, viewOptions);351

Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 354–357why, one step back
354
355 // merge render() options356 if (opts) merge(options, opts);357

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

lines 358–360why, one step back
358 // merge render() .locals
359 if (opts && opts.locals) merge(options, opts.locals);360

Added res.render() status support backTj Holowaychukbefore adoptiond152e7e

lines 361–363why, one step back
361 // status support
362 if (options.status) this.statusCode = options.status;363

Fixed view hintsTj Holowaychukbefore adoptionae10789

lines 364–366why, one step back
364 // capture attempts
365 options.attempts = [];366

misc refactoringTj Holowaychukbefore adoption11250d2

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 368–386why, one step back
368 , layout = options.layout;
369370 // Layout support371 if (true === layout || undefined === layout) {372 layout = 'layout';373 }374375 // Default execution scope to a plain object376 options.scope = options.scope || {};377378 // Populate view379 options.parentView = parent;380381 // "views" setting382 options.root = root;383384 // "view engine" setting385 options.defaultEngine = app.set('view engine');386

Added charset option for render()Tj Holowaychukbefore adoptionf327455

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

lines 389–414why, one step back
389
390 // Dynamic helper support391 if (false !== options.dynamicHelpers) {392 // cache393 if (!this.__dynamicHelpers) {394 this.__dynamicHelpers = {};395 for (var key in dynamicHelpers) {396 this.__dynamicHelpers[key] = dynamicHelpers[key].call(397 this.app398 , this.req399 , this);400 }401 }402403 // apply404 merge(options, this.__dynamicHelpers);405 }406407 // Merge view helpers408 union(options, helpers);409410 // Always expose partial() as a local411 options.partial = function(path, opts){412 return renderPartial(self, path, opts, options, view);413 };414

Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e

lines 415–418why, one step back
415 // View lookup
416 options.hint = app.enabled('hints');417 view = exports.compile(view, app.cache, cid, options);418 options.filename = view.path;

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

line 419why, one step back
419

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

lines 420–424why, one step back
420 // layout helper
421 options.layout = function(path){422 layout = path;423 };424

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

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

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 427why, one step back
427

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 428–443why, one step back
428 // layout expected
429 if (layout) {430 options.isLayout = true;431 options.layout = false;432 options.body = str;433 this.render(layout, options, fn, view, true);434 // partial return435 } else if (partial) {436 return str;437 // render complete, and 438 // callback given439 } else if (fn) {440 fn(null, str);441 // respond442 } else {443 this.send(str);

Added res.cookie() maxAge supportTj Holowaychukbefore adoption4c274c5

line 444why, one step back
444 }

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

line 445why, one step back
445}

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 446–456why, one step back
446
447/**448 * Hint at view path resolution, outputting the449 * paths that Express has tried.450 *451 * @api private452 */453454function hintAtViewPaths(view, options) {455 console.error();456 console.error('failed to locate view "' + view.view + '", tried:');

Fixed view hintsTj Holowaychukbefore adoptionae10789

lines 457–459why, one step back
457 options.attempts.forEach(function(path){
458 console.error(' - %s', path);459 });

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 460–461why, one step back
460 console.error();
461}