Skip to content
Myr

view.js

This file

Path

lib/view.js

415 lines · 9.8 KiB · JavaScript

Last changed by

misc refactoringTj Holowaychukbefore adoption89383ec

History of this file

Take it

The walk stopped after 200 commits: lines older than 66a6214 point at it.

fixing more examplesTj Holowaychukbefore adoption66a6214

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 union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758

line 19why, one step back
19 , union = require('./utils').union

fixing more examplesTj Holowaychukbefore adoption66a6214

line 20why, one step back
20 , merge = utils.merge

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

lines 21–22why, one step back
21 , http = require('http')
22 , res = http.ServerResponse.prototype;

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 23–44why, one step back
23
24/**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 back
45 * Partial render helper.

fixing more examplesTj Holowaychukbefore adoption66a6214

line 46why, one step back
46 *

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

line 47why, one step back
47 * @api private

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 48–49why, one step back
48 */
49

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

lines 50–52why, one step back
50function renderPartial(res, view, options, parentLocals, parent){
51 var collection, object, locals;52

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 53–58why, one step back
53 // Inherit parent view extension when not present
54 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 back
59 // collection
60 if (options.collection) {61 collection = options.collection;62 delete options.collection;63 } else if ('length' in options) {64 collection = options;65 options = {};

fixing more examplesTj Holowaychukbefore adoption66a6214

line 66why, one step back
66 }

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

lines 67–81why, one step back
67
68 // 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 }

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 82–86why, one step back
82 } else {
83 options = {};84 }8586 // Inherit locals from parent

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

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 88–89why, one step back
88
89 // Merge locals

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

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 91–96why, one step back
91
92 // Partials dont need layouts93 options.renderPartial = true;94 options.layout = false;9596 // Deduce name from view path

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 98–100why, one step back
98
99 // Render partial100 function render(){

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

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

fixing more examplesTj Holowaychukbefore adoption66a6214

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

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

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

fixing more examplesTj Holowaychukbefore adoption66a6214

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

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

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

fixing more examplesTj Holowaychukbefore adoption66a6214

line 106why, one step back
106 } else {

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

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 108–109why, one step back
108 }
109 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 111–115why, one step back
111 }
112113 // Collection support114 if (collection) {115 var len = collection.length

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 116–120why, one step back
116 , buf = ''
117 , keys118 , key119 , val;120

fixing more examplesTj Holowaychukbefore adoption66a6214

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

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 122–139why, one step back
122
123 if ('number' == typeof len || Array.isArray(collection)) {124 for (var i = 0; i < len; ++i) {125 val = collection[i];126 options.firstInCollection = i == 0;127 options.indexInCollection = i;128 options.lastInCollection = i == len - 1;129 object = val;130 buf += render();131 } 132 } else {133 keys = Object.keys(collection);134 len = keys.length;135 options.collectionLength = len;136 options.collectionKeys = keys;137 for (var i = 0; i < len; ++i) {138 key = keys[i];139 val = collection[key];

misc refactoringTj Holowaychukbefore adoption89383ec

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

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

lines 141–146why, one step back
141 options.firstInCollection = i == 0;
142 options.indexInCollection = i;143 options.lastInCollection = i == len - 1;144 object = val;145 buf += render();146 }

fixing more examplesTj Holowaychukbefore adoption66a6214

line 147why, one step back
147 }

Added better partial() object collection supportTj Holowaychukbefore adoption08046f7

line 148why, one step back
148

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 149–155why, one step back
149 return buf;
150 } else {151 return render();152 }153};154155/**

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

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

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

lines 159–172why, one step back
159 *
160 * Options:161 *162 * - `object` Single object with name derived from the view (unless `as` is present) 163 *164 * - `as` Variable name for each `collection` value, defaults to the view name.165 * * as: 'something' will add the `something` local variable166 * * as: this will use the collection value as the template context167 * * as: global will merge the collection value's properties with `locals`168 *169 * - `collection` Array of objects, the name is derived from the view name itself. 170 * For example _video.html_ will have a object _video_ available to it.171 *172 * @param {String} view

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

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

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

lines 175–178why, one step back
175 * @return {String}
176 * @api public177 */178

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

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

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

lines 180–183why, one step back
180 var app = this.app
181 , options = options || {}182 , parent = {};183

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 184–189why, one step back
184 // accept callback as second argument
185 if ('function' == typeof options) {186 fn = options;187 options = {};188 }189

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

lines 190–197why, one step back
190 // root "views" option
191 parent.dirname = app.set('views') || process.cwd() + '/views';192193 // utilize "view engine" option194 if (app.set('view engine')) {195 parent.extension = '.' + app.set('view engine');196 }197

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 198–204why, one step back
198 // render the partial
199 try {200 var str = renderPartial(this, view, options, null, parent);201 } catch (err) {202 if (fn) {203 fn(err);204 } else {

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

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

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

line 206why, one step back
206 }

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

line 207why, one step back
207 return;

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 208–215why, one step back
208 }
209210 // callback or transfer211 if (fn) {212 fn(null, str);213 } else {214 this.send(str);215 }

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

lines 216–218why, one step back
216};
217218/**

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 219–226why, one step back
219 * Render `view` with the given `options` and optional callback `fn`.
220 * When a callback function is given a response will _not_ be made221 * automatically, however otherwise a response of _200_ and _text/html_ is given.222 *223 * Options:224 * 225 * - `scope` Template evaluation context (the value of `this`)226 * - `debug` Output debugging information

docsTj Holowaychukbefore adoption2007407

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 228–234why, one step back
228 *
229 * @param {String} view230 * @param {Object|Function} options or callback function231 * @param {Function} fn232 * @api public233 */234

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

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

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

line 238why, one step back
238 fn = opts, opts = null;

fixing more examplesTj Holowaychukbefore adoption66a6214

line 239why, one step back
239 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 240why, one step back
240

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 241–246why, one step back
241 try {
242 return this._render(view, opts, fn, parent, sub);243 } catch (err) {244 // callback given245 if (fn) {246 fn(err);

docsTj Holowaychukbefore adoption7888cb0

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

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 249–260why, one step back
249 } else if (sub) {
250 throw err;251 // root template, next(err)252 } else {253 this.req.next(err);254 }255 }256};257258// private render()259260res._render = function(view, opts, fn, parent, sub){

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

lines 261–262why, one step back
261 var options = {}
262 , self = this

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 263–265why, one step back
263 , app = this.app
264 , helpers = app.viewHelpers265 , dynamicHelpers = app.dynamicViewHelpers

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

line 266why, one step back
266 , viewOptions = app.set('view options')

misc refactoringTj Holowaychukbefore adoption11250d2

lines 267–268why, one step back
267 , cacheViews = app.set('cache views')
268 , root = app.set('views') || process.cwd() + '/views';

fixing more examplesTj Holowaychukbefore adoption66a6214

line 269why, one step back
269

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

line 270why, one step back
270 // merge "view options"

misc refactoringTj Holowaychukbefore adoption530328a

line 271why, one step back
271 if (viewOptions) merge(options, viewOptions);

fixing more examplesTj Holowaychukbefore adoption66a6214

line 272why, one step back
272

Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a

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

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

lines 275–276why, one step back
275
276 // merge render() options

misc refactoringTj Holowaychukbefore adoption530328a

line 277why, one step back
277 if (opts) merge(options, opts);

Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710

line 278why, one step back
278

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

lines 279–281why, one step back
279 // merge render() .locals
280 if (opts && opts.locals) merge(options, opts.locals);281

Added res.render() status support backTj Holowaychukbefore adoptiond152e7e

lines 282–284why, one step back
282 // status support
283 if (options.status) this.statusCode = options.status;284

misc refactoringTj Holowaychukbefore adoption11250d2

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 286–292why, one step back
286 , layout = options.layout;
287288 // Layout support289 if (true === layout || undefined === layout) {290 layout = 'layout';291 }292

Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4

lines 293–294why, one step back
293 // Default execution scope to a plain object
294 options.scope = options.scope || {};

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 295–302why, one step back
295
296 // Populate view297 options.parentView = parent;298299 // "views" setting300 options.root = root;301302 // "view engine" setting

utilizing app.set()Tj Holowaychukbefore adoption334604d

line 303why, one step back
303 options.defaultEngine = app.set('view engine');

fixing more examplesTj Holowaychukbefore adoption66a6214

line 304why, one step back
304

Added charset option for render()Tj Holowaychukbefore adoptionf327455

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

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 307–325why, one step back
307
308 // Dynamic helper support309 if (false !== options.dynamicHelpers) {310 // cache311 if (!this.__dynamicHelpers) {312 this.__dynamicHelpers = {};313 for (var key in dynamicHelpers) {314 this.__dynamicHelpers[key] = dynamicHelpers[key].call(315 this.app316 , this.req317 , this);318 }319 }320321 // apply322 merge(options, this.__dynamicHelpers);323 }324325 // Merge view helpers

Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758

line 326why, one step back
326 union(options, helpers);

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 327–329why, one step back
327
328 // Always expose partial() as a local329 options.partial = function(path, opts){

Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c

line 330why, one step back
330 return renderPartial(self, path, opts, options, view);

fixing more examplesTj Holowaychukbefore adoption66a6214

lines 331–332why, one step back
331 };
332

Fixed .filename view engine option [reported by drudge]Tj Holowaychukbefore adoptiona6fdc1b

lines 333–335why, one step back
333 // Provide filename to engine
334 options.filename = view.path;335

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

lines 336–371why, one step back
336 // cached view
337 if (cache[view]) {338 view = cache[view];339 // resolve view340 } else {341 var orig = view = new View(view, options);342343 // Try _ prefix ex: ./views/_<name>.jade344 if (!view.exists) view = new View(orig.prefixPath, options);345 346 // Try index ex: ./views/user/index.jade347 if (!view.exists) view = new View(orig.indexPath, options);348349 // Try ../<name>/index ex: ../user/index.jade350 // when calling partial('user') within the same dir351 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);352353 // Try root ex: <root>/user.jade354 if (!view.exists) view = new View(orig.rootPath, options);355356 // Try root _ prefix ex: <root>/_user.jade357 if (!view.exists && partial) view = new View(view.prefixPath, options);358359 // Does not exist360 if (!view.exists) {361 if (app.enabled('hints')) hintAtViewPaths(orig, options);362 var err = new Error('failed to locate view "' + orig.view + '"');363 err.view = orig;364 throw err;365 } 366367 var engine = view.templateEngine;368 view.fn = engine.compile(view.contents, options)369 if (cacheViews) cache[orig.view] = view;370 }371

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

lines 372–376why, one step back
372 // layout helper
373 options.layout = function(path){374 layout = path;375 };376

Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29

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

fixing more examplesTj Holowaychukbefore adoption66a6214

line 379why, one step back
379

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 380–395why, one step back
380 // layout expected
381 if (layout) {382 options.isLayout = true;383 options.layout = false;384 options.body = str;385 this.render(layout, options, fn, view, true);386 // partial return387 } else if (partial) {388 return str;389 // render complete, and 390 // callback given391 } else if (fn) {392 fn(null, str);393 // respond394 } else {395 this.send(str);

fixing more examplesTj Holowaychukbefore adoption66a6214

line 396why, one step back
396 }

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

line 397why, one step back
397}

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 398–415why, one step back
398
399/**400 * Hint at view path resolution, outputting the401 * paths that Express has tried.402 *403 * @api private404 */405406function hintAtViewPaths(view, options) {407 console.error();408 console.error('failed to locate view "' + view.view + '", tried:');409 console.error(' - ' + new View(view.path, options).path);410 console.error(' - ' + new View(view.prefixPath, options).path);411 console.error(' - ' + new View(view.indexPath, options).path);412 if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);413 if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);414 console.error();415}