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 ( ? : )
1 2 3 4 5 6 |
- 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)
1 2 3 4 5 6 7 8 9 10 |
- 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.
1 2 3 4 5 6 7 8 9 |
- 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//each statement - list = ['a', 'b', 'c'] ul each item in list li= item //while loop - index = 0 ul while index < list.length li= list[index] - index++; //for loop ul - for(var i = 0, len = list.length; i < len; i += 1) { li= list[i] - } |
In my next post I will write about mixins and filters for Jade.
Leave A Comment