Bird
Raised Fist0
Laravelframework~10 mins

Blade directives in Laravel - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Blade directives
Start Blade Template
↓
Read Blade Directive
↓
Match Directive Type
↓
Evaluate
↓
Render HTML Output
↓
End Template
Blade reads each directive, decides its type (condition, loop, include), evaluates it, and renders the HTML output accordingly.
Execution Sample
Laravel
@if($user)
  <p>Welcome, {{ $user->name }}!</p>
@else
  <p>Please log in.</p>
@endif
This Blade snippet checks if a user exists and shows a welcome message or a login prompt.
Execution Table
StepDirective ReadCondition/LoopEvaluation ResultRendered Output
1@if($user)Check if $user existsTrue<p>Welcome, John!</p>
2@elseSkipped because @if was trueN/ANo output
3@endifEnd if blockN/ANo output
💡 Reached @endif, finished conditional rendering.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$usernull or User objectUser object (John)User object (John)User object (John)
Key Moments - 2 Insights
Why does the @else block not render when $user exists?
Because in the execution_table at Step 1, the @if condition is true, so Blade skips the @else block as shown in Step 2.
What happens if $user is null?
The @if condition at Step 1 would be false, so Blade would skip to the @else block and render '<p>Please log in.</p>'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is rendered at Step 1 when $user exists?
A<p>Please log in.</p>
B<p>Welcome, John!</p>
CNo output
DError message
💡 Hint
Check the Rendered Output column at Step 1 in the execution_table.
At which step does Blade decide to skip the @else block?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at the Condition/Loop and Evaluation Result columns for Step 2 in the execution_table.
If $user was null, what would change in the execution_table?
AStep 1 evaluation would be false, and Step 2 would render '<p>Please log in.</p>'
BStep 1 would render welcome message anyway
CStep 3 would render an error
DNo change
💡 Hint
Refer to the key_moments explanation about $user being null.
Concept Snapshot
@if(condition)
  // HTML if true
@else
  // HTML if false
@endif

@foreach($items as $item)
  // Loop content
@endforeach

@include('partial')

Blade directives control template logic and rendering flow simply.
Full Transcript
Blade directives in Laravel templates let you add logic like conditions and loops inside HTML. For example, @if checks a condition and shows content if true, otherwise @else content. Blade reads each directive, evaluates it, and renders the matching HTML. In the example, if $user exists, it shows a welcome message; if not, it asks to log in. The execution table shows each step: reading the directive, evaluating the condition, and rendering output. Variables like $user change state before and after evaluation. Understanding when Blade skips or renders blocks helps avoid confusion. This visual trace helps beginners see how Blade templates run step-by-step.

Practice

(1/5)
1. Which Blade directive is used to display a variable safely escaped in a Laravel view?
easy
A. @foreach($variable as $item)
B. @php $variable @endphp
C. {{ $variable }}
D. @if($variable)

Solution

  1. Step 1: Understand Blade variable display

    Blade uses double curly braces {{ }} to safely display variables with HTML escaping.
  2. Step 2: Identify correct directive for safe output

    The {{ $variable }} syntax outputs the variable safely, preventing XSS attacks.
  3. Final Answer:

    {{ $variable }} -> Option C
  4. Quick Check:

    Safe variable display = {{ $variable }} [OK]
Hint: Use double curly braces {{ }} for safe variable output [OK]
Common Mistakes:
  • Using @php for output instead of {{ }}
  • Confusing @if with variable display
  • Using @foreach for single variable output
2. Which of the following is the correct syntax to start a Blade loop over an array $items?
easy
A. @loop($items as $item)
B. @for($items as $item)
C. @while($items as $item)
D. @foreach($items as $item)

Solution

  1. Step 1: Recall Blade loop directives

    Blade uses @foreach to loop over arrays or collections.
  2. Step 2: Match correct syntax

    The correct syntax is @foreach($items as $item), which loops through each item.
  3. Final Answer:

    @foreach($items as $item) -> Option D
  4. Quick Check:

    Loop over array = @foreach [OK]
Hint: Use @foreach for looping arrays in Blade [OK]
Common Mistakes:
  • Using @loop which does not exist
  • Using @for incorrectly for arrays
  • Using @while for array loops
3. What will be the output of this Blade snippet if $count = 3?
@if($count > 5)
  Large
@elseif($count > 1)
  Medium
@else
  Small
@endif
medium
A. Large
B. Medium
C. Small
D. No output

Solution

  1. Step 1: Evaluate the condition $count > 5

    Since $count = 3, 3 > 5 is false, so skip the first block.
  2. Step 2: Evaluate the @elseif($count > 1) condition

    3 > 1 is true, so Blade outputs "Medium" and skips the @else.
  3. Final Answer:

    Medium -> Option B
  4. Quick Check:

    3 > 1 but not > 5 = Medium [OK]
Hint: Check conditions top to bottom; first true block runs [OK]
Common Mistakes:
  • Choosing Large because 3 is close to 5
  • Ignoring @elseif and picking @else
  • Thinking all blocks output
4. Identify the error in this Blade snippet:
@foreach($users as $user)
  

{{ $user->name }}

@endforeach @endfor
medium
A. Incorrect closing directive @endfor instead of @endforeach
B. Missing closing @endforeach directive
C. Incorrect variable name $user instead of $users
D. Missing opening @for directive

Solution

  1. Step 1: Check loop opening and closing directives

    The loop starts with @foreach but incorrectly ends with @endfor.
  2. Step 2: Match correct closing directive

    Blade requires @endforeach to close a @foreach loop, so @endfor is invalid here.
  3. Final Answer:

    Incorrect closing directive @endfor instead of @endforeach -> Option A
  4. Quick Check:

    Match @foreach with @endforeach [OK]
Hint: Close @foreach with @endforeach, not @endfor [OK]
Common Mistakes:
  • Using @endfor to close @foreach
  • Confusing $user and $users variables
  • Missing closing directive entirely
5. You want to include a Blade template named header.blade.php inside your main view. Which directive correctly includes it and passes a variable $title with value 'Welcome'?
hard
A. @include('header', ['title' => 'Welcome'])
B. @include('header', $title = 'Welcome')
C. @include('header', ['title' => Welcome])
D. @include('header', title = 'Welcome')

Solution

  1. Step 1: Recall correct syntax for @include with data

    Blade's @include accepts the view name and an associative array of data.
  2. Step 2: Check array syntax for passing variables

    The correct way is @include('header', ['title' => 'Welcome']) with quotes around string values.
  3. Final Answer:

    @include('header', ['title' => 'Welcome']) -> Option A
  4. Quick Check:

    Pass variables as array in @include [OK]
Hint: Pass variables as array in @include(['key' => value]) [OK]
Common Mistakes:
  • Passing variable without array brackets
  • Missing quotes around string values
  • Using assignment syntax inside @include
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard