Skip to content
Myr

view.js

This file

Path

lib/view.js

372 lines · 8.9 KiB · JavaScript

Last changed by

Fixed up index view path alternativeTj Holowaychukbefore adoption220d88d

History of this file

Take it

The walk stopped after 200 commits: lines older than 05c2946 point at it.

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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;

Added qs submodTj Holowaychukbefore adoption05c2946

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.

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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 }

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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;

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

line 106why, one step back
106 } else {

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

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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;

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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

Added qs submodTj Holowaychukbefore adoption05c2946

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;

Added qs submodTj Holowaychukbefore adoption05c2946

line 215why, one step back
215 }

refactored res.render()Tj Holowaychukbefore adoption2ff991b

line 216why, one step back
216

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

lines 217–218why, one step back
217 var options = {}
218 , self = this

Added qs submodTj Holowaychukbefore adoption05c2946

lines 219–221why, one step back
219 , app = this.app
220 , helpers = app.viewHelpers221 , dynamicHelpers = app.dynamicViewHelpers

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

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

Added qs submodTj Holowaychukbefore adoption05c2946

line 224why, one step back
224

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

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

misc refactoringTj Holowaychukbefore adoption530328a

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

Added qs submodTj Holowaychukbefore adoption05c2946

line 227why, one step back
227

Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a

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

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

lines 230–231why, one step back
230
231 // merge render() options

misc refactoringTj Holowaychukbefore adoption530328a

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

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

line 233why, one step back
233

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

lines 234–236why, one step back
234 // merge render() .locals
235 if (opts && opts.locals) merge(options, opts.locals);236

Added res.render() status support backTj Holowaychukbefore adoptiond152e7e

lines 237–239why, one step back
237 // status support
238 if (options.status) this.statusCode = options.status;239

Added qs submodTj Holowaychukbefore adoption05c2946

lines 240–241why, one step back
240 // Defaults
241 var self = this

utilizing app.set()Tj Holowaychukbefore adoption334604d

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

Added qs submodTj Holowaychukbefore adoption05c2946

lines 243–250why, one step back
243 , partial = options.renderPartial
244 , layout = options.layout;245246 // Layout support247 if (true === layout || undefined === layout) {248 layout = 'layout';249 }250

Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4

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

Added qs submodTj Holowaychukbefore adoption05c2946

lines 253–260why, one step back
253
254 // Populate view255 options.parentView = parent;256257 // "views" setting258 options.root = root;259260 // "view engine" setting

utilizing app.set()Tj Holowaychukbefore adoption334604d

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

Added qs submodTj Holowaychukbefore adoption05c2946

line 262why, one step back
262

Added charset option for render()Tj Holowaychukbefore adoptionf327455

lines 263–265why, one step back
263 // charset option
264 if (options.charset) this.charset = options.charset;265

Added qs submodTj Holowaychukbefore adoption05c2946

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

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

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

Added qs submodTj Holowaychukbefore adoption05c2946

line 268why, one step back
268

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

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

Added qs submodTj Holowaychukbefore adoption05c2946

line 271why, one step back
271

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

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

misc refactorTj Holowaychukbefore adoption110b0fe

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

Added View#upIndexPathTj Holowaychukbefore adoption32be4a7

line 274why, one step back
274

Fixed up index view path alternativeTj Holowaychukbefore adoption220d88d

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

misc refactorTj Holowaychukbefore adoption110b0fe

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

fixing testsTj Holowaychukbefore adoption2b907e9

line 278why, one step back
278

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

lines 279–280why, one step back
279 // Try layout relative to the "views" dir
280 if (!view.exists && options.isLayout) view = new View(orig.rootPath, options);

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

lines 281–283why, one step back
281
282 // Does not exist283 if (!view.exists) {

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 284–285why, one step back
284 if (app.enabled('hints')) hintAtViewPaths(orig, options);
285 throw new Error('failed to locate view "' + orig.view + '"');

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

line 286why, one step back
286 }

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

line 287why, one step back
287

Added qs submodTj Holowaychukbefore adoption05c2946

lines 288–305why, one step back
288 // Dynamic helper support
289 if (false !== options.dynamicHelpers) {290 // cache291 if (!this.__dynamicHelpers) {292 this.__dynamicHelpers = {};293 for (var key in dynamicHelpers) {294 this.__dynamicHelpers[key] = dynamicHelpers[key].call(295 this.app296 , this.req297 , this);298 }299 }300301 // apply302 merge(options, this.__dynamicHelpers);303 }304305 // Merge view helpers

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

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

Added qs submodTj Holowaychukbefore adoption05c2946

lines 307–309why, one step back
307
308 // Always expose partial() as a local309 options.partial = function(path, opts){

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

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

Added qs submodTj Holowaychukbefore adoption05c2946

lines 311–312why, one step back
311 };
312

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

lines 313–315why, one step back
313 // Provide filename to engine
314 options.filename = view.path;315

Added qs submodTj Holowaychukbefore adoption05c2946

lines 316–323why, one step back
316 // Attempt render
317 try {318 var engine = view.templateEngine319 , template = cacheTemplates320 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))321 : engine.compile(view.contents, options)322 , str = template.call(options.scope, options);323

refactored res.render()Tj Holowaychukbefore adoption2ff991b

lines 324–328why, one step back
324 // layout expected
325 if (layout) {326 options.isLayout = true;327 options.layout = false;328 options.body = str;

pass the functionTj Holowaychukbefore adoptionbd1ab7a

line 329why, one step back
329 this.render(layout, options, fn, view, true);

refactored res.render()Tj Holowaychukbefore adoption2ff991b

lines 330–352why, one step back
330 // partial return
331 } else if (partial) {332 return str;333 // render complete, and 334 // callback given335 } else if (fn) {336 fn(null, str);337 // respond338 } else {339 this.send(str);340 }341342 } catch (err) {343 // callback given344 if (fn) {345 fn(err);346 // unwind to root call347 } else if (sub) {348 throw err;349 // root template, next(err)350 } else {351 this.req.next(err);352 }

Added qs submodTj Holowaychukbefore adoption05c2946

lines 353–354why, one step back
353 }
354};

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 355–372why, one step back
355
356/**357 * Hint at view path resolution, outputting the358 * paths that Express has tried.359 *360 * @api private361 */362363function hintAtViewPaths(view, options) {364 console.error();365 console.error('failed to locate view "' + view.view + '", tried:');366 console.error(' - ' + new View(view.path, options).path);367 console.error(' - ' + new View(view.prefixPath, options).path);368 console.error(' - ' + new View(view.indexPath, options).path);369 if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);370 if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);371 console.error();372}