Conditional Statement in Pug
Conditional statements allow you to execute specific blocks of code based on conditions. If the condition meets then a particular block of statements will be executed otherwise it will execute another block of statement that satisfies that particular condition.
Several methods used to perform Conditional Statements in Pug are:
- if statement: Executes a code block if a specified condition is true.
- else statement: Executes a block of code if the same condition of the preceding if statement is false.
- else if statement: Adds more conditions to the if statement, allowing multiple alternative conditions to be tested.
Syntax
if condition
// pug code
else
// pug code
Syntax for else if
if condition
// pug code
else if condition
//pug code
else
//pug code
Conditionals in Pug View Engine
Pug supports JavaScript natively, hence using JavaScript expressions, we can format HTML code. This approach allows us to reuse static web pages which have dynamic data. Angular brackets are not used while specifying the tags.