Case

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

  1. - var friends = 10
  2. case friends
  3. when 0
  4. p you have no friends
  5. when 1
  6. p you have a friend
  7. default
  8. p you have #{friends} friends
  1. <p>you have 10 friends</p>

Case Fall Through

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

  1. - var friends = 0
  2. case friends
  3. when 0
  4. when 1
  5. p you have very few friends
  6. default
  7. p you have #{friends} friends
  1. <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:

  1. - var friends = 0
  2. case friends
  3. when 0
  4. - break
  5. when 1
  6. p you have very few friends
  7. default
  8. p you have #{friends} friends

Block Expansion

Block expansion may also be used:

  1. - var friends = 1
  2. case friends
  3. when 0: p you have no friends
  4. when 1: p you have a friend
  5. default: p you have #{friends} friends
  1. <p>you have a friend</p>