aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-messages/index.js
blob: 099ffa3d2580646ad1c263049145e751020724a0 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
/*!
 * Express - Contrib - messages
 * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
 * MIT Licensed
 */

module.exports = function (req, res) {
  return function (template, locals) {
    var flash = req.flash()
      , types = Object.keys(flash)
      , output = '';

    if (types.length) {
      if (template) {
        locals = locals || {};
        locals.messages = flash;
        res.render(template, locals, function (err, html) {
          if (html) {
            output = html;
          }
        });
      } else {
        var buf = [];
        buf.push('<div id="messages">');
        types.forEach(function (type) {
          var msgs = flash[type];
          if (msgs) {
            buf.push('  <ul class="' + type + '">');
            msgs.forEach(function (msg) {
              buf.push('    <li>' + msg + '</li>');
            });
            buf.push('  </ul>');
          }
        });
        buf.push('</div>');
        output = buf.join('\n');
      }
    }

    return output;
  };
};