Skip to content
Myr

Added `app.local(name, val)`

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 bcb2cda · Browse files · 3 files+37 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+37 added−20 removed

  • other:+37 added−20 removed

3 files+37 added−20 removed

modified:docs/guide.md+7 added-9 removed
1157 unmodified lines
1158When mounted, _res.redirect()_ will respect the mount-point. For example if a blog app is mounted at _/blog_, the following will redirect to _/blog/posts_:11591160 res.redirect('/posts');11611162### app.helpers(obj)1162### app.locals(obj)11631164Registers static view helpers.1164Registers static view locals.11651166 app.helpers({1166 app.locals({1167 name: function(first, last){ return first + ', ' + last }1168 , firstName: 'tj'1169 , lastName: 'holowaychuk'1170 });
7 unmodified lines
11781179 - `settings` the app's settings object1180 - `layout(path)` specify the layout from within a view11811182This method is aliased as _app.locals()_.1182### app.dynamicLocals(obj)11831184### app.dynamicHelpers(obj)11851186Registers dynamic view helpers. Dynamic view helpers1184Registers dynamic view locals. Dynamic locals1185are simply functions which accept _req_, _res_, and are1188evaluated against the _Server_ instance before a view is rendered. The _return value_ of this function1186evaluated against the _Server_ instance before a view is rendered, and are unique to that specific request. The _return value_ of this function1187becomes the local variable it is associated with.11881191 app.dynamicHelpers({1189 app.dynamicLocals({1190 session: function(req, res){1191 return req.session;1192 }1193 });
169 unmodified lines
modified:lib/http.js+25 added-4 removed
14 unmodified lines
15 , Router = require('./router')16 , view = require('./view')17 , toArray = require('./utils').toArray18 , methods = router.methods.concat('del', 'all')19 , res = require('./response')20 , url = require('url')21 , utils = connect.utils;2223/**
230 unmodified lines
254 return this;255};256257/**257 * Register the given view helpers `obj`. This method258 * can be called several times to apply additional helpers.258 * Assign the view local variable `name` to `val` or return the259 * local previously assigned to `name`.260 *261 * @param {String} name262 * @param {Mixed} val263 * @return {Mixed} val264 * @api public265 */266267app.local = function(name, val){268 this._locals = this._locals || {};269 return undefined === val270 ? this._locals[name]271 : this._locals[name] = val;272};273274/**275 * Assign several locals with the given `obj`,276 * or return the locals.277 *278 * @param {Object} obj261 * @return {Server} for chaining279 * @return {Object|HTTPServer}280 * @api public281 */282283app.locals = function(obj){266 utils.merge(this._locals, obj);284 if (0 == arguments.length) return this._locals;285 for (var key in obj) {286 this.local(key, obj[key]);287 }288 return this;289};290291/**
230 unmodified lines
modified:lib/response.js+5 added-7 removed
440 unmodified lines
441 * Assign several locals with the given `obj`,442 * or return the locals.443 *444 * @param {Object} obj445 * @return {Object|Undefined}445 * @return {Object|ServerResponse}446 * @api public447 */448449res.locals = function(obj){450 if (obj) {451 for (var key in obj) {452 this.local(key, obj[key]);453 }454 } else {455 return this._locals;450 if (0 == arguments.length) return this._locals;451 for (var key in obj) {452 this.local(key, obj[key]);453 }454 return this;455};