Conditionals

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):

- var user = { description: 'foo bar baz' }
- var authorised = false
#user
  if user.description
    h2.green Description
    p.description= user.description
  else if authorised
    h2.blue Description
    p.description.
      User has no description,
      why not add one...
  else
    h2.red Description
    p.description User has no description
<div id="user">
  <h2 class="green">Description</h2>
  <p class="description">foo bar baz</p>
</div>

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

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

© Pug authors
Licensed under the MIT license.
https://pugjs.org/language/conditionals.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部