Overview
The if
statement is a fundamental control flow structure in Zare templating.
It allows a program to make decisions and execute code only when a specified condition is true.
Syntax (Generic Form)
@if (condition) {
// code to execute if condition is true
}
How It Works
-
Evaluate the condition inside the parentheses
()
it can be any valid js condition. -
If the condition is true, the code inside the
{}
block is executed. -
If the condition is false, the code block is skipped.
-
If you want to use Parameter Expressions or Functions in condition you can directly access them. Don’t need to wrap parameter expressions in
@(…)
and call any function without@
.
Example
serve (
@if (1 == 1) {
<p>One is equals to 1</p>
}
@if (!user) {
<button>Login</button>
}
)
Output:
<p>One is equals to 1</p>
<!-- If you no passed the user while rendering the template or it is null -->
<button>Login</button>
Key Points
- The condition must return true or false (a Boolean value).
Last updated on