Fixed dynamic helper in flash example
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 348fa40 · Browse files · 3 files·+47 added−24 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+47 added−24 removed
- other:+47 added−24 removed
Provenance
- Commit
b4e7557- Tree
ad33b14- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
348fa40
3 files changed+47 added−24 removed
added:examples/flash/views/partials/messages.ejs+12 added
1+<% if (hasMessages) { %>2+ <div id="messages">3+ <% for (var type in types) { %>4+ <h2><%= type %> messages</h2>5+ <ul id="<%= type %>">6+ <% types[type].forEach(function(msg){ %>7+ <li><%= msg %></li>8+ <% }) %>9+ </ul>10+ <% } %>11+ </div>12+<% } %>modified:examples/flash/app.js+23 added-14 removed
15 unmodified lines1617181920202122232425262728242526272829303132333435362930313233343536373839404142434445464748498 unmodified lines15 unmodified lines
app.set('views', __dirname + '/views');app.set('view engine', 'ejs');
//// Dynamic helpers are functions which are executed// on each view render, unless dynamicHelpers is false.
// So for example we do not need to call messages() in our// template, "messages" will be populated with the return// value of this function.
app.dynamicHelpers({ messages: function(req, res){ var types = req.flash(), keys = Object.keys(types), len = keys.length; if (len) { return '<div id="messages">' + keys.map(function(key){ var msgs = types[key]; return '<ul id="messages-' + key + '">' + msgs.map(function(msg){ return '<li>' + msg + '</li>'; }).join('\n') + '</ul>'; }).join('\n') + '</div>'; } else { return ''; } // Grab the flash messages var messages = req.flash(); // We will render the "messages.ejs" partial return res.partial('messages', { // Our target object is our messages object: messages, // We want it to be named "types" in the partial // since they are keyed like this: // { info: ['foo'], error: ['bar']} as: 'types', // Pass a local named "hasMessages" so we can easily // check if we have any messages at all locals: { hasMessages: Object.keys(messages).length }, // We dont want dynamicHelpers in this partial, as // it would cause infinite recursion dynamicHelpers: false }); }});
app.get('/', function(req, res){8 unmodified linesmodified:lib/express/view.js+12 added-10 removed
207 unmodified lines208 options.locals = options.locals || {};209 options.locals.__filename = options.filename = path;210 211 // Dynamic helper support212+ if (options.dynamicHelpers !== false) {212- var keys = Object.keys(dynamicHelpers);213- for (var i = 0, len = keys.length; i < len; ++i) {214- var key = keys[i],215- val = dynamicHelpers[key];216- if (typeof val === 'function') {217- helpers[key] = val.call(218- this.app, 219- this.req, 220- this, 221- this.req.params.path);213+ var keys = Object.keys(dynamicHelpers);214+ for (var i = 0, len = keys.length; i < len; ++i) {215+ var key = keys[i],216+ val = dynamicHelpers[key];217+ if (typeof val === 'function') {218+ helpers[key] = val.call(219+ this.app, 220+ this.req, 221+ this, 222+ this.req.params.path);223+ }224 }225 }226 227 // Always expose partial() as a local46 unmodified lines