Concept Flow - Blade template syntax
Start Blade Template
Parse Blade Directives
Evaluate PHP Code
Render HTML Output
Send to Browser
Blade templates are parsed by Laravel to convert directives and PHP code into HTML that the browser can display.
@if($user)
<p>Hello, {{ $user->name }}!</p>
@else
<p>Welcome, guest!</p>
@endif| Step | Blade Directive | Condition/Expression | Evaluation Result | Rendered Output |
|---|---|---|---|---|
| 1 | @if | $user (object) | True | <p>Hello, John!</p> |
| 2 | Display | $user->name | John | John |
| 3 | @else | - | Skipped | - |
| 4 | @endif | - | End if | - |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| $user | null or object | object (John) | object (John) | object (John) |
| $user->name | N/A | John | John | John |
Blade template syntax uses directives like @if, @else, and {{ }} to control output.
Conditions are evaluated in PHP, and matching blocks render HTML.
{{ }} safely outputs variables with escaping.
@else runs only if @if condition is false.
Blade compiles templates into PHP before sending HTML to browser.{{ }} to display variables safely by escaping HTML.{{ $variable }} uses {{ $variable }}, which is the correct Blade syntax.@ for control structures like conditionals.@if(condition).$name = 'Alice'?
<p>Hello, {{ $name }}!</p>
<p>@if($name == 'Alice')
Welcome back!
@endif</p>{{ $name }} outputs 'Alice' safely inside the first paragraph.$name == 'Alice' is true, so 'Welcome back!' is shown inside the second paragraph.@foreach($items as $item)
<p>{{ $item }}</p>
@endforeachd@endforeach.@endforeachd which is invalid and causes a syntax error.@foreach($users as $user)
@if($user->email_verified)
<p>{{ $user->name }} - {{ $user->email }}</p>
@endif
@endforeach
B) @foreach($users as $user)
@if($user[email_verified])
<p>{{ $user->name }} - {{ $user->email }}</p>
@endif
@endforeach
C) @foreach($users as $user)
@if($user->email_verified == true
<p>{{ $user->name }} - {{ $user->email }}</p>
@endif
@endforeach
D) @foreach($users as $user)
@if($user->email_verified = true)
<p>{{ $user->name }} - {{ $user->email }}</p>
@endif
@endforeachPass the test and this pattern moves unknown