expresssails.jspugconnect-flash

Condition for req.flash inside Jade template (Sails)


Inside my sails.js application, I am passing flash messages inside my Jade templates. I want to display these messages inside a <section> html5 tag, but I don't want this tag to be present if there is no flash message.

Here is my Jade code :

- var flash = req.flash('error')
if error || !!flash
  section.err= error
    | #{flash}

But this doesn't work: it displays right my flash messages, but also puts everytime the <section> tag (even if the flash var contains no message).

Node : req.flash('msgtype') is flushed at every call, so I had to put it inside a variable.


Solution

  • I have finally found how to manage this, by testing flash's length :

    - var flash = req.flash('error')
    if error || flash.length != 0
      section.err= error
        | #{flash}
    

    Inspired from https://github.com/balderdashy/sails/issues/1054#issuecomment-29045509