view.js
This file
Path
lib/view.js
462 lines · 10.6 KiB · JavaScript
Last changed by
Merge branch 'refactor/dev-deps'Tj Holowaychukbefore adoption7999386
History of this fileTake it
The walk stopped after 200 commits: lines older than f327455 point at it.
Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 1–36why, 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')18 , partial = require('./view/partial')19 , union = require('./utils').union20 , merge = utils.merge21 , http = require('http')22 , res = http.ServerResponse.prototype;2324/**25 * Expose constructors.26 */2728exports = module.exports = View;2930/**31 * Export template engine registrar.32 */3334exports.register = View.register;3536/**Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e
lines 37–70why, one step back37 * Lookup and compile `view` with cache support by supplying38 * both the `cache` object and `cid` string,39 * followed by `options` passed to `exports.lookup()`.40 *41 * @param {String} view42 * @param {Object} cache43 * @param {Object} cid44 * @param {Object} options45 * @return {View}46 * @api private47 */4849exports.compile = function(view, cache, cid, options){50 if (cache && cid && cache[cid]) return cache[cid];5152 // lookup53 view = exports.lookup(view, options);5455 // hints56 if (!view.exists) {57 if (options.hint) hintAtViewPaths(view.original, options);58 var err = new Error('failed to locate view "' + view.original.view + '"');59 err.view = view.original;60 throw err;61 } 6263 // compile64 view.fn = view.templateEngine.compile(view.contents, options);65 cache[cid] = view;66 67 return view;68};6970/**added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
lines 71–72why, one step back71 * Lookup `view`, returning an instanceof `View`.72 *Updated jade submoduleTj Holowaychukbefore adoptioneeb7754
lines 73–76why, one step back73 * Options:74 *75 * - `root` root directory path76 * - `defaultEngine` default template enginedocsTj Holowaychukbefore adoption8636dee
line 77why, one step back77 * - `parentView` parent `View` objectMerge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e
lines 78–79why, one step back78 * - `cache` cache object79 * - `cacheid` optional cache idUpdated jade submoduleTj Holowaychukbefore adoptioneeb7754
line 80why, one step back80 *lookup docsTj Holowaychukbefore adoption1b199b7
lines 81–88why, one step back81 * Lookup:82 *83 * - partial `_<name>` 84 * - any `<name>/index` 85 * - non-layout `../<name>/index` 86 * - any `<root>/<name>` 87 * - partial `<root>/_<name>` 88 *added exports.lookup(view, options) to view.jsTj Holowaychukbefore adoption3043672
lines 89–121why, one step back89 * @param {String} view90 * @param {Object} options91 * @return {View}92 * @api private93 */9495exports.lookup = function(view, options){96 var orig = view = new View(view, options);9798 // Try _ prefix ex: ./views/_<name>.jade99 if (partial) {100 view = new View(orig.prefixPath, options);101 if (!view.exists) view = orig;102 }103104 // Try index ex: ./views/user/index.jade105 if (!view.exists) view = new View(orig.indexPath, options);106107 // Try ../<name>/index ex: ../user/index.jade108 // when calling partial('user') within the same dir109 if (!view.exists && !options.isLayout) view = new View(orig.upIndexPath, options);110111 // Try root ex: <root>/user.jade112 if (!view.exists) view = new View(orig.rootPath, options);113114 // Try root _ prefix ex: <root>/_user.jade115 if (!view.exists && partial) view = new View(view.prefixPath, options);116117 view.original = orig;118 return view;119};120121/**Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 122–126why, one step back122 * Partial render helper.123 *124 * @api private125 */126Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 127–129why, one step back127function renderPartial(res, view, options, parentLocals, parent){128 var collection, object, locals;129Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 130–135why, one step back130 // Inherit parent view extension when not present131 if (parent && !~view.indexOf('.')) {132 view += parent.extension;133 }134135 if (options) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 136–142why, one step back136 // collection137 if (options.collection) {138 collection = options.collection;139 delete options.collection;140 } else if ('length' in options) {141 collection = options;142 options = {};Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 143why, one step back143 }Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
lines 144–158why, one step back144145 // locals146 if (options.locals) {147 locals = options.locals;148 delete options.locals;149 }150151 // object152 if ('Object' != options.constructor.name) {153 object = options;154 options = {};155 } else if (undefined != options.object) {156 object = options.object;157 delete options.object;158 }Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 159–163why, one step back159 } else {160 options = {};161 }162163 // Inherit locals from parentChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 164why, one step back164 union(options, parentLocals);Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 165–166why, one step back165 166 // Merge localsChanged; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 167why, one step back167 if (locals) merge(options, locals);Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 168–177why, one step back168169 // Partials dont need layouts170 options.renderPartial = true;171 options.layout = false;172173 // Deduce name from view path174 var name = options.as || partial.resolveObjectName(view);175176 // Render partial177 function render(){Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 178why, one step back178 if (object) {Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 179why, one step back179 if ('string' == typeof name) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 180why, one step back180 options[name] = object;Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 181why, one step back181 } else if (name === global) {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 182why, one step back182 merge(options, object);Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 183why, one step back183 } else {Changed; partial() "locals" are now optionalTj Holowaychukbefore adoption909914f
line 184why, one step back184 options.scope = object;Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 185–186why, one step back185 }186 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 187why, one step back187 return res.render(view, options, null, parent, true);Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 188–192why, one step back188 }189190 // Collection support191 if (collection) {192 var len = collection.lengthAdded better partial() object collection supportTj Holowaychukbefore adoption08046f7
lines 193–197why, one step back193 , buf = ''194 , keys195 , key196 , val;197Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 198why, one step back198 options.collectionLength = len;Added better partial() object collection supportTj Holowaychukbefore adoption08046f7
lines 199–216why, one step back199200 if ('number' == typeof len || Array.isArray(collection)) {201 for (var i = 0; i < len; ++i) {202 val = collection[i];203 options.firstInCollection = i == 0;204 options.indexInCollection = i;205 options.lastInCollection = i == len - 1;206 object = val;207 buf += render();208 } 209 } else {210 keys = Object.keys(collection);211 len = keys.length;212 options.collectionLength = len;213 options.collectionKeys = keys;214 for (var i = 0; i < len; ++i) {215 key = keys[i];216 val = collection[key];misc refactoringTj Holowaychukbefore adoption89383ec
line 217why, one step back217 options.keyInCollection = key;Added better partial() object collection supportTj Holowaychukbefore adoption08046f7
lines 218–223why, one step back218 options.firstInCollection = i == 0;219 options.indexInCollection = i;220 options.lastInCollection = i == len - 1;221 object = val;222 buf += render();223 }Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 224why, one step back224 }Added better partial() object collection supportTj Holowaychukbefore adoption08046f7
line 225why, one step back225Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 226–232why, one step back226 return buf;227 } else {228 return render();229 }230};231232/**Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 233–235why, one step back233 * Render `view` partial with the given `options`. Optionally a 234 * callback `fn(err, str)` may be passed instead of writing to235 * the socket.Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 236–249why, one step back236 *237 * Options:238 *239 * - `object` Single object with name derived from the view (unless `as` is present) 240 *241 * - `as` Variable name for each `collection` value, defaults to the view name.242 * * as: 'something' will add the `something` local variable243 * * as: this will use the collection value as the template context244 * * as: global will merge the collection value's properties with `locals`245 *246 * - `collection` Array of objects, the name is derived from the view name itself. 247 * For example _video.html_ will have a object _video_ available to it.248 *249 * @param {String} viewAdded res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 250–251why, one step back250 * @param {Object|Array|Function} options, collection, callback, or object251 * @param {Function} fnAdded charset option for render()Tj Holowaychukbefore adoptionf327455
lines 252–255why, one step back252 * @return {String}253 * @api public254 */255Added res.partial() callback supportTj Holowaychukbefore adoption723c908
line 256why, one step back256res.partial = function(view, options, fn){Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 257–258why, one step back257 var app = this.app258 , options = options || {}misc refactor of res.partial()Tj Holowaychukbefore adoption72384b0
line 259why, one step back259 , viewEngine = app.set('view engine')Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 260–261why, one step back260 , parent = {};261Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 262–267why, one step back262 // accept callback as second argument263 if ('function' == typeof options) {264 fn = options;265 options = {};266 }267Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 268–271why, one step back268 // root "views" option269 parent.dirname = app.set('views') || process.cwd() + '/views';270271 // utilize "view engine" optionmisc refactor of res.partial()Tj Holowaychukbefore adoption72384b0
line 272why, one step back272 if (viewEngine) parent.extension = '.' + viewEngine;Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 273why, one step back273Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 274–280why, one step back274 // render the partial275 try {276 var str = renderPartial(this, view, options, null, parent);277 } catch (err) {278 if (fn) {279 fn(err);280 } else {Fixed res.partial(); next(err) when no callback is given [reported by aheckmann]Tj Holowaychukbefore adoption28ba9e8
line 281why, one step back281 this.req.next(err);Added res.partial() callback supportTj Holowaychukbefore adoption723c908
line 282why, one step back282 }Fixed res.partial(); next(err) when no callback is given [reported by aheckmann]Tj Holowaychukbefore adoption28ba9e8
line 283why, one step back283 return;Added res.partial() callback supportTj Holowaychukbefore adoption723c908
lines 284–291why, one step back284 }285286 // callback or transfer287 if (fn) {288 fn(null, str);289 } else {290 this.send(str);291 }Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 292–310why, one step back292};293294/**295 * Render `view` with the given `options` and optional callback `fn`.296 * When a callback function is given a response will _not_ be made297 * automatically, however otherwise a response of _200_ and _text/html_ is given.298 *299 * Options:300 * 301 * - `scope` Template evaluation context (the value of `this`)302 * - `debug` Output debugging information303 * - `status` Response status code304 *305 * @param {String} view306 * @param {Object|Function} options or callback function307 * @param {Function} fn308 * @api public309 */310refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 311why, one step back311res.render = function(view, opts, fn, parent, sub){Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 312why, one step back312 // support callback function as second argrefactored res.render()Tj Holowaychukbefore adoption2ff991b
line 313why, one step back313 if ('function' == typeof opts) {Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 314–315why, one step back314 fn = opts, opts = null;315 }refactored res.render()Tj Holowaychukbefore adoption2ff991b
line 316why, one step back316wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 317–322why, one step back317 try {318 return this._render(view, opts, fn, parent, sub);319 } catch (err) {320 // callback given321 if (fn) {322 fn(err);docsTj Holowaychukbefore adoption7888cb0
lines 323–324why, one step back323 // unwind to root call to prevent324 // several next(err) callswrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 325–336why, one step back325 } else if (sub) {326 throw err;327 // root template, next(err)328 } else {329 this.req.next(err);330 }331 }332};333334// private render()335336res._render = function(view, opts, fn, parent, sub){Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 337–339why, one step back337 var options = {}338 , self = this339 , app = this.appviewHelpers -> _localsTj Holowaychukbefore adoptioneac6665
line 340why, one step back340 , helpers = app._localsAdded charset option for render()Tj Holowaychukbefore adoptionf327455
lines 341–342why, one step back341 , dynamicHelpers = app.dynamicViewHelpers342 , viewOptions = app.set('view options')misc refactoringTj Holowaychukbefore adoption11250d2
line 343why, one step back343 , root = app.set('views') || process.cwd() + '/views';Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 344why, one step back344Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c
line 345why, one step back345 // cache idMerge branch 'refactor/dev-deps'Tj Holowaychukbefore adoption7999386
lines 346–348why, one step back346 var cid = app.enabled('view cache')347 ? view + (parent ? ':' + parent.path : '')348 : false;Fixed template caching collision issue. Closes #644Tj Holowaychukbefore adoption470774c
line 349why, one step back349Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 350–352why, one step back350 // merge "view options"351 if (viewOptions) merge(options, viewOptions);352Added res.locals(obj)Tj Holowaychukbefore adoption0b1378a
lines 353–354why, one step back353 // merge res._locals354 if (this._locals) merge(options, this._locals);Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 355–364why, one step back355356 // merge render() options357 if (opts) merge(options, opts);358359 // merge render() .locals360 if (opts && opts.locals) merge(options, opts.locals);361362 // status support363 if (options.status) this.statusCode = options.status;364Fixed view hintsTj Holowaychukbefore adoptionae10789
lines 365–367why, one step back365 // capture attempts366 options.attempts = [];367misc refactoringTj Holowaychukbefore adoption11250d2
line 368why, one step back368 var partial = options.renderPartialAdded charset option for render()Tj Holowaychukbefore adoptionf327455
lines 369–415why, one step back369 , layout = options.layout;370371 // Layout support372 if (true === layout || undefined === layout) {373 layout = 'layout';374 }375376 // Default execution scope to a plain object377 options.scope = options.scope || {};378379 // Populate view380 options.parentView = parent;381382 // "views" setting383 options.root = root;384385 // "view engine" setting386 options.defaultEngine = app.set('view engine');387388 // charset option389 if (options.charset) this.charset = options.charset;390391 // Dynamic helper support392 if (false !== options.dynamicHelpers) {393 // cache394 if (!this.__dynamicHelpers) {395 this.__dynamicHelpers = {};396 for (var key in dynamicHelpers) {397 this.__dynamicHelpers[key] = dynamicHelpers[key].call(398 this.app399 , this.req400 , this);401 }402 }403404 // apply405 merge(options, this.__dynamicHelpers);406 }407408 // Merge view helpers409 union(options, helpers);410411 // Always expose partial() as a local412 options.partial = function(path, opts){413 return renderPartial(self, path, opts, options, view);414 };415Merge branch 'refactor/views'Tj Holowaychukbefore adoption09b384e
lines 416–419why, one step back416 // View lookup417 options.hint = app.enabled('hints');418 view = exports.compile(view, app.cache, cid, options);419 options.filename = view.path;Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
line 420why, one step back420Merge branch 'feature/view-layout-control'Tj Holowaychukbefore adoption6a58c71
lines 421–425why, one step back421 // layout helper422 options.layout = function(path){423 layout = path;424 };425Performance improved with better view cachingTj Holowaychukbefore adoption4b4de29
lines 426–427why, one step back426 // render427 var str = view.fn.call(options.scope, options);Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 428why, one step back428wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
lines 429–444why, one step back429 // layout expected430 if (layout) {431 options.isLayout = true;432 options.layout = false;433 options.body = str;434 this.render(layout, options, fn, view, true);435 // partial return436 } else if (partial) {437 return str;438 // render complete, and 439 // callback given440 } else if (fn) {441 fn(null, str);442 // respond443 } else {444 this.send(str);Added charset option for render()Tj Holowaychukbefore adoptionf327455
line 445why, one step back445 }wrap try/catch around render()Tj Holowaychukbefore adoptioncf26cf7
line 446why, one step back446}Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 447–457why, one step back447448/**449 * Hint at view path resolution, outputting the450 * paths that Express has tried.451 *452 * @api private453 */454455function hintAtViewPaths(view, options) {456 console.error();457 console.error('failed to locate view "' + view.view + '", tried:');Fixed view hintsTj Holowaychukbefore adoptionae10789
lines 458–460why, one step back458 options.attempts.forEach(function(path){459 console.error(' - %s', path);460 });Added charset option for render()Tj Holowaychukbefore adoptionf327455
lines 461–462why, one step back461 console.error();462}