Overview
The @else
statement is used with an @if
statement to specify a block of code that will execute if the @if
condition is false.
In simple words:
“If this is true, do this. ELSE, do something different.”
Syntax (Generic Form)
@if (condition) {
// code if condition is true
} @else {
// code if condition is false
}
How It Works
-
First, the
@if
condition is evaluated. -
If true, the
@if
block runs and@else
is skipped. -
If false, the
@else
block runs.
Example
serve (
@if (user) {
<h1>Welcome, @(user.username)</h1>
} @else {
<a href="/login">Login</a>
}
)
Key Points
-
@else
does NOT have a condition. It only runs when the@if
condition is false. -
There can be only one
@else
for each@if
.
Last updated on