Skip to content
Myr

view.js

This file

Path

lib/view.js

320 lines · 7.8 KiB · JavaScript

Last changed by

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

History of this file

Take it

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

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;

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

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.

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

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

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

line 50why, one step back
50function renderPartial(res, view, options, locals, parent){

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 51–67why, one step back
51 // Inherit parent view extension when not present
52 if (parent && !~view.indexOf('.')) {53 view += parent.extension;54 }5556 // Allow collection to be passed as second param57 if (options) {58 if ('length' in options) {59 options = { collection: options };60 } else if (!options.collection && !options.locals && !options.object) {61 options = { object: options };62 }63 } else {64 options = {};65 }6667 // Inherit locals from parent

Fixed partial() issue where the parent locals clobber those given directlyTj Holowaychukbefore adoption1fc520b

line 68why, one step back
68 union(options, locals);

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 69–79why, one step back
69
70 // Merge locals71 if (options.locals) {72 merge(options, options.locals);73 }7475 // Partials dont need layouts76 options.renderPartial = true;77 options.layout = false;7879 // Deduce name from view path

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 81–92why, one step back
81
82 // Render partial83 function render(){84 if (options.object) {85 if ('string' == typeof name) {86 options[name] = options.object;87 } else if (name === global) {88 merge(options, options.object);89 } else {90 options.scope = options.object;91 }92 }

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

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 94–117why, one step back
94 }
9596 // Collection support97 var collection = options.collection;98 if (collection) {99 var len = collection.length100 , buf = '';101 delete options.collection;102 options.collectionLength = len;103 for (var i = 0; i < len; ++i) {104 var val = collection[i];105 options.firstInCollection = i === 0;106 options.indexInCollection = i;107 options.lastInCollection = i === len - 1;108 options.object = val;109 buf += render();110 }111 return buf;112 } else {113 return render();114 }115};116117/**

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

lines 118–151why, one step back
118 * Render `view` partial with the given `options`.
119 *120 * Options:121 *122 * - `object` Single object with name derived from the view (unless `as` is present) 123 *124 * - `as` Variable name for each `collection` value, defaults to the view name.125 * * as: 'something' will add the `something` local variable126 * * as: this will use the collection value as the template context127 * * as: global will merge the collection value's properties with `locals`128 *129 * - `collection` Array of objects, the name is derived from the view name itself. 130 * For example _video.html_ will have a object _video_ available to it.131 *132 * @param {String} view133 * @param {Object|Array} options, collection, or object134 * @return {String}135 * @api public136 */137138res.partial = function(view, options){139 var app = this.app140 , options = options || {}141 , parent = {};142143 // root "views" option144 parent.dirname = app.set('views') || process.cwd() + '/views';145146 // utilize "view engine" option147 if (app.set('view engine')) {148 parent.extension = '.' + app.set('view engine');149 }150151 var str = renderPartial(this, view, options, null, parent);

Removed partial() / render() status and header optionsTj Holowaychukbefore adoptionaaa8415

line 152why, one step back
152 this.send(str);

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

lines 153–155why, one step back
153};
154155/**

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 156–163why, one step back
156 * Render `view` with the given `options` and optional callback `fn`.
157 * When a callback function is given a response will _not_ be made158 * automatically, however otherwise a response of _200_ and _text/html_ is given.159 *160 * Options:161 * 162 * - `scope` Template evaluation context (the value of `this`)163 * - `debug` Output debugging information

docsTj Holowaychukbefore adoption2007407

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 165–171why, one step back
165 *
166 * @param {String} view167 * @param {Object|Function} options or callback function168 * @param {Function} fn169 * @api public170 */171

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

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

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

lines 173–175why, one step back
173 // support callback function as second arg
174 if (typeof opts === 'function') {175 fn = opts, opts = null;

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 176–177why, one step back
176 }
177

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

lines 178–179why, one step back
178 var options = {}
179 , self = this

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 180–182why, one step back
180 , app = this.app
181 , helpers = app.viewHelpers182 , dynamicHelpers = app.dynamicViewHelpers

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

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

line 185why, one step back
185

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

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

misc refactoringTj Holowaychukbefore adoption530328a

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

line 188why, one step back
188

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

line 189why, one step back
189 // merge res.locals

misc refactoringTj Holowaychukbefore adoption530328a

line 190why, one step back
190 if (this.locals) merge(options, this.locals);

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

lines 191–192why, one step back
191
192 // merge render() options

misc refactoringTj Holowaychukbefore adoption530328a

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

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

line 194why, one step back
194

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

lines 195–197why, one step back
195 // merge render() .locals
196 if (opts && opts.locals) merge(options, opts.locals);197

Added res.render() status support backTj Holowaychukbefore adoptiond152e7e

lines 198–200why, one step back
198 // status support
199 if (options.status) this.statusCode = options.status;200

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 201–202why, one step back
201 // Defaults
202 var self = this

utilizing app.set()Tj Holowaychukbefore adoption334604d

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 204–211why, one step back
204 , partial = options.renderPartial
205 , layout = options.layout;206207 // Layout support208 if (true === layout || undefined === layout) {209 layout = 'layout';210 }211

Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 214–221why, one step back
214
215 // Populate view216 options.parentView = parent;217218 // "views" setting219 options.root = root;220221 // "view engine" setting

utilizing app.set()Tj Holowaychukbefore adoption334604d

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 223–224why, one step back
223
224 // Populate view

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

line 226why, one step back
226

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

line 229why, one step back
229

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

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

misc refactorTj Holowaychukbefore adoption110b0fe

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

Added View#upIndexPathTj Holowaychukbefore adoption32be4a7

line 232why, one step back
232

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

line 233why, one step back
233 // Try ../name ../user from within ./user

misc refactorTj Holowaychukbefore adoption110b0fe

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

fixing testsTj Holowaychukbefore adoption2b907e9

line 235why, one step back
235

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

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

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

lines 238–240why, one step back
238
239 // Does not exist240 if (!view.exists) {

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

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

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

line 243why, one step back
243 }

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

line 244why, one step back
244

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 245–262why, one step back
245 // Dynamic helper support
246 if (false !== options.dynamicHelpers) {247 // cache248 if (!this.__dynamicHelpers) {249 this.__dynamicHelpers = {};250 for (var key in dynamicHelpers) {251 this.__dynamicHelpers[key] = dynamicHelpers[key].call(252 this.app253 , this.req254 , this);255 }256 }257258 // apply259 merge(options, this.__dynamicHelpers);260 }261262 // Merge view helpers

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

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 264–266why, one step back
264
265 // Always expose partial() as a local266 options.partial = function(path, opts){

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

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 268–299why, one step back
268 };
269270 function error(err) {271 if (fn) {272 fn(err);273 } else {274 self.req.next(err);275 }276 }277278 // Attempt render279 try {280 var engine = view.templateEngine281 , template = cacheTemplates282 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))283 : engine.compile(view.contents, options)284 , str = template.call(options.scope, options);285 } catch (err) {286 return error(err);287 }288289 // Layout support290 if (layout) {291 options.isLayout = true;292 options.layout = false;293 options.body = str;294 self.render(layout, options, fn, view);295 } else if (partial) {296 return str;297 } else if (fn) {298 fn(null, str);299 } else {

Removed partial() / render() status and header optionsTj Holowaychukbefore adoptionaaa8415

line 300why, one step back
300 this.send(str);

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 301–302why, one step back
301 }
302};

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 303–320why, one step back
303
304/**305 * Hint at view path resolution, outputting the306 * paths that Express has tried.307 *308 * @api private309 */310311function hintAtViewPaths(view, options) {312 console.error();313 console.error('failed to locate view "' + view.view + '", tried:');314 console.error(' - ' + new View(view.path, options).path);315 console.error(' - ' + new View(view.prefixPath, options).path);316 console.error(' - ' + new View(view.indexPath, options).path);317 if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);318 if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);319 console.error();320}