Skip to content
Myr

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 file

Take 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 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

styling for stylus exampleTj Holowaychukbefore adoptione958988

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;

styling for stylus exampleTj Holowaychukbefore adoptione958988

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.

styling for stylus exampleTj Holowaychukbefore adoptione958988

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

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 = {};

styling for stylus exampleTj Holowaychukbefore adoptione958988

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 }

styling for stylus exampleTj Holowaychukbefore adoptione958988

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);

styling for stylus exampleTj Holowaychukbefore adoptione958988

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);

styling for stylus exampleTj Holowaychukbefore adoptione958988

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);

styling for stylus exampleTj Holowaychukbefore adoptione958988

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) {

styling for stylus exampleTj Holowaychukbefore adoptione958988

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;

styling for stylus exampleTj Holowaychukbefore adoptione958988

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);

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 106why, one step back
106 } else {

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

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

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);

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 111–122why, one step back
111 }
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 back
123 object = val;

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 124–132why, one step back
124 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 back
133 * 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 back
136 *
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} view

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

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

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

lines 152–155why, one step back
152 * @return {String}
153 * @api public154 */155

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

line 156why, one step back
156res.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 back
157 var app = this.app
158 , options = options || {}159 , parent = {};160

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 161–166why, one step back
161 // accept callback as second argument
162 if ('function' == typeof options) {163 fn = options;164 options = {};165 }166

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

lines 167–174why, one step back
167 // root "views" option
168 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 }174

Added res.partial() callback supportTj Holowaychukbefore adoption723c908

lines 175–191why, one step back
175 // render the partial
176 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 back
192};
193194/**

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 195–202why, one step back
195 * 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 information

docsTj Holowaychukbefore adoption2007407

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 204–210why, one step back
204 *
205 * @param {String} view206 * @param {Object|Function} options or callback function207 * @param {Function} fn208 * @api public209 */210

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

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

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

refactored res.render()Tj Holowaychukbefore adoption2ff991b

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

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

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 215why, one step back
215 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 216why, one step back
216

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 217–235why, one step back
217 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 back
236 var options = {}
237 , self = this

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 238–240why, one step back
238 , app = this.app
239 , helpers = app.viewHelpers240 , dynamicHelpers = app.dynamicViewHelpers

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

lines 241–242why, one step back
241 , viewOptions = app.set('view options')
242 , cacheTemplates = app.set('cache views');

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 243why, one step back
243

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

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

misc refactoringTj Holowaychukbefore adoption530328a

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 246why, one step back
246

Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a

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

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

lines 249–250why, one step back
249
250 // merge render() options

misc refactoringTj Holowaychukbefore adoption530328a

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

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

line 252why, one step back
252

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

lines 253–255why, one step back
253 // merge render() .locals
254 if (opts && opts.locals) merge(options, opts.locals);255

Added res.render() status support backTj Holowaychukbefore adoptiond152e7e

lines 256–258why, one step back
256 // status support
257 if (options.status) this.statusCode = options.status;258

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 259–260why, one step back
259 // Defaults
260 var self = this

utilizing app.set()Tj Holowaychukbefore adoption334604d

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 262–269why, one step back
262 , partial = options.renderPartial
263 , layout = options.layout;264265 // Layout support266 if (true === layout || undefined === layout) {267 layout = 'layout';268 }269

Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 272–279why, one step back
272
273 // Populate view274 options.parentView = parent;275276 // "views" setting277 options.root = root;278279 // "view engine" setting

utilizing app.set()Tj Holowaychukbefore adoption334604d

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 281why, one step back
281

Added charset option for render()Tj Holowaychukbefore adoptionf327455

lines 282–284why, one step back
282 // charset option
283 if (options.charset) this.charset = options.charset;284

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 285why, one step back
285 // Populate view

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

line 286why, one step back
286 var orig = view = new View(view, options);

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 287why, one step back
287

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

lines 288–289why, one step back
288 // Try _ prefix ex: ./views/_user.jade
289 if (!view.exists) view = new View(orig.prefixPath, options);

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 290why, one step back
290

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

line 291why, one step back
291 // Try index ex: ./views/user/index.jade

misc refactorTj Holowaychukbefore adoption110b0fe

line 292why, one step back
292 if (!view.exists) view = new View(orig.indexPath, options);

Added View#upIndexPathTj Holowaychukbefore adoption32be4a7

line 293why, one step back
293

Fixed up index view path alternativeTj Holowaychukbefore adoption220d88d

lines 294–295why, one step back
294 // Try ../<name>/index ex: ../user/index.jade
295 // when calling partial('user') within the same dir

misc refactorTj Holowaychukbefore adoption110b0fe

line 296why, one step back
296 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);

fixing testsTj Holowaychukbefore adoption2b907e9

line 297why, one step back
297

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

lines 298–299why, one step back
298 // 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 back
300
301 // Does not exist302 if (!view.exists) {

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 303–304why, one step back
303 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 back
305 }

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

line 306why, one step back
306

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 307–324why, one step back
307 // Dynamic helper support
308 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 helpers

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

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 326–328why, one step back
326
327 // 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 back
329 return renderPartial(self, path, opts, options, view);

styling for stylus exampleTj Holowaychukbefore adoptione958988

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

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

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

styling for stylus exampleTj Holowaychukbefore adoptione958988

line 335why, one step back
335 // Attempt render

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 336–340why, one step back
336 var engine = view.templateEngine
337 , 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 back
341

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

lines 342–357why, one step back
342 // layout expected
343 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 back
358 }

wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7

line 359why, one step back
359}

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 360–377why, one step back
360
361/**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}