X

Jade Node.js Template Engine, Conditional Logic Statements Part 4

Jade

Jade Node.js Template Engine, Conditional Logic Statements

In my previous post I have talked about databinding with jade, i.e. feeding data to your template. In this post I will write about how one can use conditional and logical statements (for, if, while statements) in jade template engine.

Ternary Operation

The conditional ternary operator can be applied in Jade as follow ( ? : )

- time = "AM"
- greeting = (time == "AM") ? "Morning" : "Evening"

p Good #{greeting}
- var morning = false
body(class=morning?'AM':'PM')

Conditional Statements

Jade has built in (if, else statements, and also provides the unless statement)

- authorized = false
div
   if authorized
     p Welcome User
   else
     p User must login

div 
  unless authorized
    p You must login

Case statements

The case statement is more of a shorthand for the switch statement in Jade.

- input = 10
case input
  when 0
  when 1
    p input is 0 or 1
  when 10
    p input is 10
  default
    p input is #{input}

Iterators

Jade provides (for, each, while as loops)

//each statement
- list = ['a', 'b', 'c']
ul 
  each item in list
    li= item

//while loop
- index = 0
ul 
  while index 

In my next post I will write about mixins and filters for Jade.

Taswar Bhatti:
Related Post