2 spaces in view.js
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 9033b0d · Browse files · 2 files·+156 added−157 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+156 added−157 removed
- other:+156 added−157 removed
Provenance
- Commit
712771a- Tree
99eb135- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9033b0d
2 files changed+156 added−157 removed
modified:docs/contrib.md+1 added-1 removed
28 unmodified lines293031323333343536373828 unmodified lines * All tests _must_ pass * Your alterations or additions _must_ include tests * Your commit(s) should be _focused_, do not commit once for several changes * Do _not_ alter release information such as the _version_, or _History.md_ * Indents are _4_ spaces. * Indents are _2_ spaces.
### Documentation
To contribute documentation edit the markdown files in _./docs_, howeverdo _not_ run _make docs_, as they will be re-built and published with each release.modified:lib/express/view.js+155 added-156 removed
7 unmodified lines8 /**9 * Module dependencies.10 */11 12-var extname = require('path').extname,13- mime = require('connect/utils').mime,14- utils = require('connect/utils'),15- http = require('http'),16- fs = require('fs');12+var extname = require('path').extname13+ , utils = require('connect').utils14+ , http = require('http')15+ , fs = require('fs')16+ , mime = utils.mime;17 18 /**19 * Cache supported template engine exports to20 * increase performance by lowering the number20 unmodified lines41 * @api private42 */43 44 function cacheViewSync(path) {45- return viewCache[path] = fs.readFileSync(path, 'utf8');45+ return viewCache[path] = fs.readFileSync(path, 'utf8');46 }47 48 /**49 * Return view root path for the given `app`.3 unmodified lines53 * @api private54 */55 56 function viewRoot(app) {57- return app.set('views') || process.cwd() + '/views';57+ return app.set('views') || process.cwd() + '/views';58 }59 60 /**61 * Register the given template engine `exports`24 unmodified lines86 * @api public87 */88 89 exports.register = function(ext, exports) {90- cache[ext] = exports;90+ cache[ext] = exports;91 };92 93 /**94 * Render `view` partial with the given `options`.15 unmodified lines110 * @api public111 */112 113 http.ServerResponse.prototype.partial = function(view, options, ext){114- // Inherit parent view extension when not present115- if (ext && view.indexOf('.') < 0) {116- view += ext;117- }114+ // Inherit parent view extension when not present115+ if (ext && view.indexOf('.') < 0) {116+ view += ext;117+ }118 119- // Allow collection to be passed as second param120- if (options && options.hasOwnProperty('length')) {121- options = { collection: options };122- }119+ // Allow collection to be passed as second param120+ if (options && options.hasOwnProperty('length')) {121+ options = { collection: options };122+ }123 124- // Defaults125- options = options || {};126- options.locals = options.locals || {};127- options.partial = true;128- options.layout = false;124+ // Defaults125+ options = options || {};126+ options.locals = options.locals || {};127+ options.partial = true;128+ options.layout = false;129 130- // Collection support131- var collection = options.collection;132- if (collection) {133- var name = options.as || view.split('.')[0],134- len = collection.length,135- buf = '';136- delete options.collection;137- options.locals.collectionLength = len;138- for (var i = 0, len = collection.length; i < len; ++i) {139- var val = collection[i];140- options.locals.firstInCollection = i === 0;141- options.locals.indexInCollection = i;142- options.locals.lastInCollection = i === len - 1;143- options.object = val;144- buf += this.partial(view, options);145- }146- return buf;147- } else {148- if (options.object) {149- var name = options.as || view.split('.')[0];150- if (typeof name === 'string') {151- options.locals[name] = options.object;152- } else if (name === global) {153- utils.merge(options.locals, options.object);154- } else {155- options.scope = options.object;156- }157- }158- return this.render(view, options);130+ // Collection support131+ var collection = options.collection;132+ if (collection) {133+ var name = options.as || view.split('.')[0]134+ , len = collection.length135+ , buf = '';136+ delete options.collection;137+ options.locals.collectionLength = len;138+ for (var i = 0, len = collection.length; i < len; ++i) {139+ var val = collection[i];140+ options.locals.firstInCollection = i === 0;141+ options.locals.indexInCollection = i;142+ options.locals.lastInCollection = i === len - 1;143+ options.object = val;144+ buf += this.partial(view, options);145 }146+ return buf;147+ } else {148+ if (options.object) {149+ var name = options.as || view.split('.')[0];150+ if (typeof name === 'string') {151+ options.locals[name] = options.object;152+ } else if (name === global) {153+ utils.merge(options.locals, options.object);154+ } else {155+ options.scope = options.object;156+ }157+ }158+ return this.render(view, options);159+ }160 };161 162 /**163 * Render `view` with the given `options` and optional callback `fn`.17 unmodified lines181 * @api public182 */183 184 http.ServerResponse.prototype.render = function(view, options, fn){185- // Support callback function as second arg186- if (typeof options === 'function') {187- fn = options, options = {};188- }189- 190- var options = options || {},191- viewOptions = this.app.set('view options'),192- defaultEngine = this.app.set('view engine');185+ // Support callback function as second arg186+ if (typeof options === 'function') {187+ fn = options, options = {};188+ }189+ 190+ var options = options || {}191+ , app = this.app192+ , viewOptions = app.settings['view options']193+ , defaultEngine = app.settings['view engine'];194 194- // Mixin "view options"195- if (viewOptions) options.__proto__ = viewOptions;195+ // Mixin "view options"196+ if (viewOptions) options.__proto__ = viewOptions;197 197- // Support "view engine" setting198- if (view.indexOf('.') < 0 && defaultEngine) {199- view += '.' + defaultEngine;200- }198+ // Support "view engine" setting199+ if (view.indexOf('.') < 0 && defaultEngine) {200+ view += '.' + defaultEngine;201+ }202 202- // Defaults203- var self = this,204- helpers = this.app.viewHelpers,205- dynamicHelpers = this.app.dynamicViewHelpers,206- root = viewRoot(this.app),207- ext = extname(view),208- partial = options.partial,209- layout = options.layout === undefined ? true : options.layout,210- layout = layout === true211- ? 'layout' + ext212- : layout;203+ // Defaults204+ var self = this205+ , helpers = this.app.viewHelpers206+ , dynamicHelpers = this.app.dynamicViewHelpers207+ , root = viewRoot(this.app)208+ , ext = extname(view)209+ , partial = options.partial210+ , layout = options.layout === undefined ? true : options.layout211+ , layout = layout === true212+ ? 'layout' + ext213+ : layout;214 214- // Allow layout name without extension215- if (typeof layout === 'string' && layout.indexOf('.') < 0) {216- layout += ext;217- }215+ // Allow layout name without extension216+ if (typeof layout === 'string' && layout.indexOf('.') < 0) {217+ layout += ext;218+ }219 219- // Default execution scope to the response220- options.scope = options.scope || this.req;220+ // Default execution scope to the response221+ options.scope = options.scope || this.req;222 222- // Auto-cache in production223- if (this.app.set('env') === 'production') {224- options.cache = true;225- }223+ // Auto-cache in production224+ if (this.app.set('env') === 'production') {225+ options.cache = true;226+ }227 227- // Partials support228- if (options.partial) {229- root = this.app.set('partials')230- ? this.app.set('partials')231- : root + '/partials';232- }228+ // Partials support229+ if (options.partial) {230+ root = app.settings.partials || root + '/partials';231+ }232 234- // View path235- var path = view[0] === '/'236- ? view237- : root + '/' + view;233+ // View path234+ var path = view[0] === '/'235+ ? view236+ : root + '/' + view;237 239- // Pass filename to the engine and view240- options.locals = options.locals || {};241- options.locals.__filename = options.filename = path;238+ // Pass filename to the engine and view239+ options.locals = options.locals || {};240+ options.locals.__filename = options.filename = path;241 243- // Dynamic helper support244- if (options.dynamicHelpers !== false) {245- // cache246- if (!this.__dynamicHelpers) {247- this.__dynamicHelpers = {};248- var keys = Object.keys(dynamicHelpers);249- for (var i = 0, len = keys.length; i < len; ++i) {250- var key = keys[i],251- val = dynamicHelpers[key];252- if (typeof val === 'function') {253- this.__dynamicHelpers[key] = val.call(254- this.app, 255- this.req, 256- this);257- }258- }242+ // Dynamic helper support243+ if (options.dynamicHelpers !== false) {244+ // cache245+ if (!this.__dynamicHelpers) {246+ this.__dynamicHelpers = {};247+ var keys = Object.keys(dynamicHelpers);248+ for (var i = 0, len = keys.length; i < len; ++i) {249+ var key = keys[i]250+ , val = dynamicHelpers[key];251+ if (typeof val === 'function') {252+ this.__dynamicHelpers[key] = val.call(253+ this.app254+ , this.req255+ , this);256 }260-261- // apply262- utils.merge(helpers, this.__dynamicHelpers);257+ }258 }259 260+ // apply261+ utils.merge(helpers, this.__dynamicHelpers);262+ }263+265- // Always expose partial() as a local266- options.locals.partial = function(view, options){267- return self.partial.call(self, view, options, ext);268- };264+ // Always expose partial() as a local265+ options.locals.partial = function(view, options){266+ return self.partial.call(self, view, options, ext);267+ };268 270- // Merge view helpers271- options.locals.__proto__ = helpers;269+ // Merge view helpers270+ options.locals.__proto__ = helpers;271 273- function error(err) {274- if (fn) {275- fn(err);276- } else {277- self.req.next(err);278- }272+ function error(err) {273+ if (fn) {274+ fn(err);275+ } else {276+ self.req.next(err);277 }278+ }279 281- // Cache contents282- try {283- var str = (options.cache ? viewCache[path] : null) || cacheViewSync(path);284- } catch (err) {285- return error(err);286- }280+ // Cache contents281+ try {282+ var str = (options.cache ? viewCache[path] : null) || cacheViewSync(path);283+ } catch (err) {284+ return error(err);285+ }286 288- // Cache template engine exports289- var engine = cache[ext] || (cache[ext] = require(ext.substr(1)));287+ // Cache template engine exports288+ var engine = cache[ext] || (cache[ext] = require(ext.substr(1)));289 291- // Attempt render292- try {293- var str = engine.render(str, options);294- } catch (err) {295- return error(err);296- }290+ // Attempt render291+ try {292+ var str = engine.render(str, options);293+ } catch (err) {294+ return error(err);295+ }296 298- // Layout support299- if (layout) {300- options.layout = false;301- options.locals.body = str;302- options.isLayout = true;303- self.render(layout, options, fn);304- } else if (partial) {305- return str;306- } else if (fn) {307- fn(null, str);308- } else {309- this.send(str, options.headers, options.status);310- }297+ // Layout support298+ if (layout) {299+ options.layout = false;300+ options.locals.body = str;301+ options.isLayout = true;302+ self.render(layout, options, fn);303+ } else if (partial) {304+ return str;305+ } else if (fn) {306+ fn(null, str);307+ } else {308+ this.send(str, options.headers, options.status);309+ }310 };