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
@ifcondition is evaluated. -
If true, the
@ifblock runs and@elseis skipped. -
If false, the
@elseblock runs.
Example
serve (
@if (user) {
<h1>Welcome, @(user.username)</h1>
} @else {
<a href="/login">Login</a>
}
)Key Points
-
@elsedoes NOT have a condition. It only runs when the@ifcondition is false. -
There can be only one
@elsefor each@if.
Last updated on