Case

Case

The case statement is a shorthand for JavaScript’s switch statement. It takes the following form:

- var friends = 10
case friends
  when 0
    p you have no friends
  when 1
    p you have a friend
  default
    p you have #{friends} friends
<p>you have 10 friends</p>

Case Fall Through

You can use fall through, just as you would in a JavaScript switch statement.

- var friends = 0
case friends
  when 0
  when 1
    p you have very few friends
  default
    p you have #{friends} friends
<p>you have very few friends</p>

The difference, however, is a fall through in JavaScript happens whenever a break statement is not explicitly included; in Pug, it only happens when a block is completely missing.

If you would like to not output anything in a specific case, add an explicit unbuffered break:

- var friends = 0
case friends
  when 0
    - break
  when 1
    p you have very few friends
  default
    p you have #{friends} friends

Block Expansion

Block expansion may also be used:

<p>you have a friend</p>

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

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部