Added dynamic view helper support. Closes #349
Before adoption: this commit came in with the history the project adopted, so no landing records it.
via app.helpers()
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent eedf54b · Browse files · 4 files·+63 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+63 added−0 removed
- tests:+22 added−0 removed
- other:+41 added−0 removed
Provenance
- Commit
9ef8ea9- Tree
3b4c4a4- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
eedf54b
4 files changed+63 added−0 removed
modified:lib/express/server.js+23 added
97 unmodified lines98 return this;99 };100 101 /**102+ * Register the given view helpers `obj`. This method103+ * can be called several times to apply additional helpers.104+ *105+ * @param {Object} obj106+ * @return {Server} for chaining107+ * @api public108+ */109+110+Server.prototype.helpers = function(obj){111+ var keys = Object.keys(obj);112+ for (var i = 0, len = keys.length; i < len; ++i) {113+ var key = keys[i],114+ val = obj[key];115+ if (typeof val === 'function') {116+ view.dynamicHelpers[key] = val;117+ } else {118+ view.helpers[key] = val;119+ }120+ }121+ return this;122+};123+124+/**125 * Assign a custom exception handler callback `fn`.126 *127 * @param {Function} fn128 * @return {Server} for chaining93 unmodified linesmodified:lib/express/view.js+18 added
51 unmodified lines52 53 var helpers = exports.helpers = {};54 55 /**56+ * Dynamic view helper functions.57+ * 58+ * @type Object59+ */60+61+var dynamicHelpers = exports.dynamicHelpers = {};62+63+/**64 * Cache view at the given `path`.65 *66 * @param {String} path67 * @return {String}208 unmodified lines276 // Pass filename to the engine and view277 options.locals = options.locals || {};278 options.locals.__filename = options.filename = path;279 280+ // Dynamic helper support281+ var keys = Object.keys(dynamicHelpers);282+ for (var i = 0, len = keys.length; i < len; ++i) {283+ var key = keys[i],284+ val = dynamicHelpers[key];285+ if (typeof val === 'function') {286+ helpers[key] = val.call(this.app, this.req, this, this.req.params.path);287+ }288+ }289+290 // Always expose partial() as a local291 options.locals.partial = function(view, options){292 return self.partial.call(self, view, options, ext);293 };42 unmodified linesadded:test/fixtures/dynamic-helpers.jade+1 added
1+p #{session.name} #{lastName}modified:test/view.test.js+21 added
118 unmodified lines119 { url: '/' },120 { body: '<cool><p>Welcome</p></cool>' });121 },122 123+ 'test #render() dynamic view helpers': function(assert){124+ var app = express.createServer();125+ app.set('views', __dirname + '/fixtures');126+127+ app.helpers({128+ lastName: 'holowaychuk',129+ session: function(req){130+ return req.session;131+ }132+ });133+ 134+ app.get('/', function(req, res){135+ req.session = { name: 'tj' };136+ res.render('dynamic-helpers.jade', { layout: false });137+ });138+ 139+ assert.response(app,140+ { url: '/' },141+ { body: '<p>tj holowaychuk</p>' });142+ },143+ 144 'test #partial()': function(assert){145 var app = express.createServer();146 app.set('views', __dirname + '/fixtures');147 134 unmodified lines