Skip to content
Myr

view.js

This file

Path

lib/view.js

323 lines · 7.9 KiB · JavaScript

Last changed by

Added charset option for render()Tj Holowaychukbefore adoptionf327455

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

line 223why, one step back
223

Added charset option for render()Tj Holowaychukbefore adoptionf327455

lines 224–226why, one step back
224 // charset option
225 if (options.charset) this.charset = options.charset;226

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

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

Removed Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb

line 228why, one step back
228 var orig = view = new View(view, 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

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

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

line 232why, one step back
232

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

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

misc refactorTj Holowaychukbefore adoption110b0fe

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

Added View#upIndexPathTj Holowaychukbefore adoption32be4a7

line 235why, one step back
235

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

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

misc refactorTj Holowaychukbefore adoption110b0fe

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

fixing testsTj Holowaychukbefore adoption2b907e9

line 238why, one step back
238

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

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

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

lines 241–243why, one step back
241
242 // Does not exist243 if (!view.exists) {

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

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

Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261

line 246why, one step back
246 }

Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6

line 247why, one step back
247

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

lines 248–265why, one step back
248 // Dynamic helper support
249 if (false !== options.dynamicHelpers) {250 // cache251 if (!this.__dynamicHelpers) {252 this.__dynamicHelpers = {};253 for (var key in dynamicHelpers) {254 this.__dynamicHelpers[key] = dynamicHelpers[key].call(255 this.app256 , this.req257 , this);258 }259 }260261 // apply262 merge(options, this.__dynamicHelpers);263 }264265 // Merge view helpers

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

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

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

lines 267–269why, one step back
267
268 // Always expose partial() as a local269 options.partial = function(path, opts){

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

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

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

lines 271–302why, one step back
271 };
272273 function error(err) {274 if (fn) {275 fn(err);276 } else {277 self.req.next(err);278 }279 }280281 // Attempt render282 try {283 var engine = view.templateEngine284 , template = cacheTemplates285 ? cache[view.path] || (cache[view.path] = engine.compile(view.contents, options))286 : engine.compile(view.contents, options)287 , str = template.call(options.scope, options);288 } catch (err) {289 return error(err);290 }291292 // Layout support293 if (layout) {294 options.isLayout = true;295 options.layout = false;296 options.body = str;297 self.render(layout, options, fn, view);298 } else if (partial) {299 return str;300 } else if (fn) {301 fn(null, str);302 } else {

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

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

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

lines 304–305why, one step back
304 }
305};

hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c

lines 306–323why, one step back
306
307/**308 * Hint at view path resolution, outputting the309 * paths that Express has tried.310 *311 * @api private312 */313314function hintAtViewPaths(view, options) {315 console.error();316 console.error('failed to locate view "' + view.view + '", tried:');317 console.error(' - ' + new View(view.path, options).path);318 console.error(' - ' + new View(view.prefixPath, options).path);319 console.error(' - ' + new View(view.indexPath, options).path);320 if (!options.isLayout) console.error(' - ' + new View(view.upIndexPath, options).path);321 if (options.isLayout) console.error(' - ' + new View(view.rootPath, options).path);322 console.error();323}