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 fileTake it
The walk stopped after 200 commits: lines older than 05c2946 point at it.
Added qs submodTj Holowaychukbefore adoption05c2946
lines 1–17why, one step back12/*!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 back18 , partial = require('./view/partial')Added union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 19why, one step back19 , union = require('./utils').unionAdded qs submodTj Holowaychukbefore adoption05c2946
line 20why, one step back20 , merge = utils.mergeAdded res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 21–22why, one step back21 , http = require('http')22 , res = http.ServerResponse.prototype;Added qs submodTj Holowaychukbefore adoption05c2946
lines 23–44why, one step back2324/**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 back45 * Partial render helper.Added qs submodTj Holowaychukbefore adoption05c2946
line 46why, one step back46 *Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
line 47why, one step back47 * @api privateAdded qs submodTj Holowaychukbefore adoption05c2946
lines 48–49why, one step back48 */49Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 50–52why, one step back50function renderPartial(res, view, options, parentLocals, parent){51 var collection, object, locals;52Added qs submodTj Holowaychukbefore adoption05c2946
lines 53–58why, one step back53 // Inherit parent view extension when not present54 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 back59 // collection60 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 back66 }Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 67–81why, one step back6768 // 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 back82 } else {83 options = {};84 }8586 // Inherit locals from parentChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 87why, one step back87 union(options, parentLocals);Added qs submodTj Holowaychukbefore adoption05c2946
lines 88–89why, one step back88 89 // Merge localsChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 90why, one step back90 if (locals) merge(options, locals);Added qs submodTj Holowaychukbefore adoption05c2946
lines 91–96why, one step back9192 // Partials dont need layouts93 options.renderPartial = true;94 options.layout = false;9596 // Deduce name from view pathRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 97why, one step back97 var name = options.as || partial.resolveObjectName(view);Added qs submodTj Holowaychukbefore adoption05c2946
lines 98–100why, one step back9899 // Render partial100 function render(){Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 101why, one step back101 if (object) {Added qs submodTj Holowaychukbefore adoption05c2946
line 102why, one step back102 if ('string' == typeof name) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 103why, one step back103 options[name] = object;Added qs submodTj Holowaychukbefore adoption05c2946
line 104why, one step back104 } else if (name === global) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 105why, one step back105 merge(options, object);Added qs submodTj Holowaychukbefore adoption05c2946
line 106why, one step back106 } else {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 107why, one step back107 options.scope = object;Added qs submodTj Holowaychukbefore adoption05c2946
lines 108–109why, one step back108 }109 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 110why, one step back110 return res.render(view, options, null, parent, true);Added qs submodTj Holowaychukbefore adoption05c2946
lines 111–122why, one step back111 }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 back123 object = val;Added qs submodTj Holowaychukbefore adoption05c2946
lines 124–132why, one step back124 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 back133 * 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 back136 *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} viewAdded res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 150–151why, one step back150 * @param {Object|Array|Function} options, collection, callback, or object151 * @param {Function} fnAdded res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 152–155why, one step back152 * @return {String}153 * @api public154 */155Added res.partial() callback supportTj Holowaychukbefore adoption723c908
line 156why, one step back156res.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 back157 var app = this.app158 , options = options || {}159 , parent = {};160Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 161–166why, one step back161 // accept callback as second argument162 if ('function' == typeof options) {163 fn = options;164 options = {};165 }166Added res.partial(), using the same interface as partial() within a view. Closes #539Tj Holowaychukbefore adoptiondd9406c
lines 167–174why, one step back167 // root "views" option168 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 }174Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 175–191why, one step back175 // render the partial176 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 back192};193194/**Added qs submodTj Holowaychukbefore adoption05c2946
lines 195–202why, one step back195 * 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 informationdocsTj Holowaychukbefore adoption2007407
line 203why, one step back203 * - `status` Response status codeAdded qs submodTj Holowaychukbefore adoption05c2946
lines 204–210why, one step back204 *205 * @param {String} view206 * @param {Object|Function} options or callback function207 * @param {Function} fn208 * @api public209 */210refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 211why, one step back211res.render = function(view, opts, fn, parent, sub){Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 212why, one step back212 // support callback function as second argrefactored res.render()Tj Holowaychukbefore adoption2ff991b
line 213why, one step back213 if ('function' == typeof opts) {Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 214why, one step back214 fn = opts, opts = null;Added qs submodTj Holowaychukbefore adoption05c2946
line 215why, one step back215 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 216why, one step back216Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 217–218why, one step back217 var options = {}218 , self = thisAdded qs submodTj Holowaychukbefore adoption05c2946
lines 219–221why, one step back219 , app = this.app220 , helpers = app.viewHelpers221 , dynamicHelpers = app.dynamicViewHelpersMerge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 222–223why, one step back222 , viewOptions = app.set('view options')223 , cacheTemplates = app.set('cache views');Added qs submodTj Holowaychukbefore adoption05c2946
line 224why, one step back224Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 225why, one step back225 // merge "view options"misc refactoringTj Holowaychukbefore adoption530328a
line 226why, one step back226 if (viewOptions) merge(options, viewOptions);Added qs submodTj Holowaychukbefore adoption05c2946
line 227why, one step back227Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a
lines 228–229why, one step back228 // merge res._locals229 if (this._locals) merge(options, this._locals);Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
lines 230–231why, one step back230231 // merge render() optionsmisc refactoringTj Holowaychukbefore adoption530328a
line 232why, one step back232 if (opts) merge(options, opts);Merge branch 'bugs/local-precedence'Tj Holowaychukbefore adoption5db5710
line 233why, one step back233Added res.render() .locals support back to aid in migration processTj Holowaychukbefore adoption07d06ad
lines 234–236why, one step back234 // merge render() .locals235 if (opts && opts.locals) merge(options, opts.locals);236Added res.render() status support backTj Holowaychukbefore adoptiond152e7e
lines 237–239why, one step back237 // status support238 if (options.status) this.statusCode = options.status;239Added qs submodTj Holowaychukbefore adoption05c2946
lines 240–241why, one step back240 // Defaults241 var self = thisutilizing app.set()Tj Holowaychukbefore adoption334604d
line 242why, one step back242 , root = app.set('views') || process.cwd() + '/views'Added qs submodTj Holowaychukbefore adoption05c2946
lines 243–250why, one step back243 , partial = options.renderPartial244 , layout = options.layout;245246 // Layout support247 if (true === layout || undefined === layout) {248 layout = 'layout';249 }250Changed default view scope to a plain objectTj Holowaychukbefore adoptiona49e1f4
lines 251–252why, one step back251 // Default execution scope to a plain object252 options.scope = options.scope || {};Added qs submodTj Holowaychukbefore adoption05c2946
lines 253–260why, one step back253254 // Populate view255 options.parentView = parent;256257 // "views" setting258 options.root = root;259260 // "view engine" settingutilizing app.set()Tj Holowaychukbefore adoption334604d
line 261why, one step back261 options.defaultEngine = app.set('view engine');Added qs submodTj Holowaychukbefore adoption05c2946
line 262why, one step back262Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 263–265why, one step back263 // charset option264 if (options.charset) this.charset = options.charset;265Added qs submodTj Holowaychukbefore adoption05c2946
line 266why, one step back266 // Populate viewRemoved Partial constructor (not used)Tj Holowaychukbefore adoptiond7488bb
line 267why, one step back267 var orig = view = new View(view, options);Added qs submodTj Holowaychukbefore adoption05c2946
line 268why, one step back268Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
lines 269–270why, one step back269 // Try _ prefix ex: ./views/_user.jade270 if (!view.exists) view = new View(orig.prefixPath, options);Added qs submodTj Holowaychukbefore adoption05c2946
line 271why, one step back271 Added view resolution hints when in developmentTj Holowaychukbefore adoptionf1f1261
line 272why, one step back272 // Try index ex: ./views/user/index.jademisc refactorTj Holowaychukbefore adoption110b0fe
line 273why, one step back273 if (!view.exists) view = new View(orig.indexPath, options);Added View#upIndexPathTj Holowaychukbefore adoption32be4a7
line 274why, one step back274Fixed up index view path alternativeTj Holowaychukbefore adoption220d88d
lines 275–276why, one step back275 // Try ../<name>/index ex: ../user/index.jade276 // when calling partial('user') within the same dirmisc refactorTj Holowaychukbefore adoption110b0fe
line 277why, one step back277 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);fixing testsTj Holowaychukbefore adoption2b907e9
line 278why, one step back278Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6
lines 279–280why, one step back279 // 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 back281282 // Does not exist283 if (!view.exists) {hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 284–285why, one step back284 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 back286 }Added layout lookup support relative to the page viewTj Holowaychukbefore adoptionb5579b6
line 287why, one step back287Added qs submodTj Holowaychukbefore adoption05c2946
lines 288–305why, one step back288 // Dynamic helper support289 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 helpersAdded union() util, taking place of merge(clone()) comboTj Holowaychukbefore adoption42d2758
line 306why, one step back306 union(options, helpers);Added qs submodTj Holowaychukbefore adoption05c2946
lines 307–309why, one step back307308 // 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 back310 return renderPartial(self, path, opts, options, view);Added qs submodTj Holowaychukbefore adoption05c2946
lines 311–312why, one step back311 };312Fixed .filename view engine option [reported by drudge]Tj Holowaychukbefore adoptiona6fdc1b
lines 313–315why, one step back313 // Provide filename to engine314 options.filename = view.path;315Added qs submodTj Holowaychukbefore adoption05c2946
lines 316–323why, one step back316 // Attempt render317 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);323refactored res.render()Tj Holowaychukbefore adoption2ff991b
lines 324–328why, one step back324 // layout expected325 if (layout) {326 options.isLayout = true;327 options.layout = false;328 options.body = str;pass the functionTj Holowaychukbefore adoptionbd1ab7a
line 329why, one step back329 this.render(layout, options, fn, view, true);refactored res.render()Tj Holowaychukbefore adoption2ff991b
lines 330–352why, one step back330 // partial return331 } 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 back353 }354};hinting at fully resolved pathsTj Holowaychukbefore adoption57b035c
lines 355–372why, one step back355356/**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}