Merge branch '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 · parents 344218f, b4e7557 · Browse files · 5 files·+88 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+88 added−10 removed
- other:+88 added−10 removed
Provenance
- Commit
7dff9dd- Tree
ad33b14- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
344218f- Parent 2
b4e7557
5 files changed+88 added−10 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+<% } %>added:examples/flash/views/index.ejs+2 added
1+<h1>Flash Message Example</h1>2+<%- messages %>added:examples/flash/views/layout.ejs+5 added
1+<html>2+ <body>3+ <%- body %>4+ </body>5+</html>added:examples/flash/app.js+57 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
/** * Module dependencies. */
var express = require('../../lib/express');
// App with session support
var app = express.createServer( express.cookieDecoder(), express.session());
// View settings
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){ // 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){ // Not very realistic notifications but illustrates usage req.flash('info', 'email queued'); req.flash('info', 'email sent'); req.flash('error', 'delivery failed'); res.render('index');});
app.listen(3000);No newline at end of filemodified: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