forked from pavelvlasov/express-dynamic-helpers-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (25 loc) · 851 Bytes
/
index.js
File metadata and controls
30 lines (25 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// express response patch for dynamicHelpers support
const response = require('express').response,
render = response.render;
module.exports = (app) => {
// add dynamicHelpers function to app object
app.dynamicHelpers = (helpers) => {
app._dynamicHelpers = app._dynamicHelpers || {};
for (let key in helpers) {
if (helpers.hasOwnProperty(key)) {
app._dynamicHelpers[key] = helpers[key];
}
}
};
};
// patch render function
response.render = function() {
var dynamicHelpers = this.app._dynamicHelpers;
if (dynamicHelpers) {
for (let key in dynamicHelpers) {
// call every helper and sign result to locals
this.locals[key] = dynamicHelpers[key].call(this.app, this.req, this);
}
}
render.apply(this, arguments);
};