index.js 1018 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*!
  2. * Express - Contrib - messages
  3. * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. module.exports = function (req, res) {
  7. return function (template, locals) {
  8. var flash = req.flash()
  9. , types = Object.keys(flash)
  10. , output = '';
  11. if (types.length) {
  12. if (template) {
  13. locals = locals || {};
  14. locals.messages = flash;
  15. res.render(template, locals, function (err, html) {
  16. if (html) {
  17. output = html;
  18. }
  19. });
  20. } else {
  21. var buf = [];
  22. buf.push('<div id="messages">');
  23. types.forEach(function (type) {
  24. var msgs = flash[type];
  25. if (msgs) {
  26. buf.push(' <ul class="' + type + '">');
  27. msgs.forEach(function (msg) {
  28. buf.push(' <li>' + msg + '</li>');
  29. });
  30. buf.push(' </ul>');
  31. }
  32. });
  33. buf.push('</div>');
  34. output = buf.join('\n');
  35. }
  36. }
  37. return output;
  38. };
  39. };