Skip to content
Myr

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

4 files+63 added−0 removed

modified:lib/express/server.js+23 added
97 unmodified lines
98 return this;99};100101/**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 */109110Server.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};123124/**125 * Assign a custom exception handler callback `fn`.126 *127 * @param {Function} fn128 * @return {Server} for chaining
93 unmodified lines
modified:lib/express/view.js+18 added
51 unmodified lines
5253var helpers = exports.helpers = {};5455/**56 * Dynamic view helper functions.57 * 58 * @type Object59 */6061var dynamicHelpers = exports.dynamicHelpers = {};6263/**64 * Cache view at the given `path`.65 *66 * @param {String} path67 * @return {String}
208 unmodified lines
276 // Pass filename to the engine and view277 options.locals = options.locals || {};278 options.locals.__filename = options.filename = path;279280 // 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 }289290 // Always expose partial() as a local291 options.locals.partial = function(view, options){292 return self.partial.call(self, view, options, ext);293 };
42 unmodified lines
added:test/fixtures/dynamic-helpers.jade+1 added
1p #{session.name} #{lastName}
modified:test/view.test.js+21 added
118 unmodified lines
119 { 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');126127 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