Conditionals

Pug’s first-class conditional syntax allows for optional parentheses.

If you’re coming from Pug v1, you may now omit the leading -. Otherwise, it’s identical (just regular JavaScript):

  1. - var user = { description: 'foo bar baz' }
  2. - var authorised = false
  3. #user
  4. if user.description
  5. h2.green Description
  6. p.description= user.description
  7. else if authorised
  8. h2.blue Description
  9. p.description.
  10. User has no description,
  11. why not add one...
  12. else
  13. h2.red Description
  14. p.description User has no description
  1. <div id="user">
  2. <h2 class="green">Description</h2>
  3. <p class="description">foo bar baz</p>
  4. </div>

Pug also provides the conditional unless, which works like a negated if. The following are equivalent:

  1. unless user.isAnonymous
  2. p You're logged in as #{user.name}
  1. if !user.isAnonymous
  2. p You're logged in as #{user.name}