سؤال

I have troubles converting the following ejs to jade:

<% if (message.length > 0) { %>
  <div class="alert alert-danger"><%= message %></div>
<% } %>

How does this block look in jade?

هل كانت مفيدة؟

المحلول

I'd write this as follows:

if message.length
  .alert.alert-danger= message

... as message is a string, message.length evaluates to a falsy value (0) only if it's an empty string. I've also used buffered output here (note that = character) so that message value will be HTML escaped.

An alternative approach is comparing message with an empty string directly:

if message !== ''
  .alert.alert-danger= message

Note that I dropped div in div.alert.alert-danger expression, as it's kind of default element for Jade templates. Have you used any other element, you'd have to start the expression with its tagname.

نصائح أخرى

Try the following snippet

https://github.com/visionmedia/jade

- if(message.length>0){
   div.alert.alert-danger #{message}
- }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top