Discover how Blade directives turn messy PHP views into clean, readable templates effortlessly!
Why Blade directives in Laravel? - Purpose & Use Cases
Imagine writing HTML pages where you have to manually add PHP code for every condition, loop, or include. For example, showing a message only if a user is logged in means mixing lots of PHP tags inside your HTML.
Manually mixing PHP and HTML gets messy fast. It's hard to read, easy to make mistakes, and difficult to maintain. You might forget to close tags or write complex PHP logic inside your views, making your code confusing.
Blade directives let you write clean, simple commands inside your HTML templates. They handle conditions, loops, includes, and more with easy-to-read syntax, keeping your views neat and your logic clear.
<?php if($user): ?> <p>Welcome, <?php echo $user->name; ?></p> <?php endif; ?>@if($user) <p>Welcome, {{ $user->name }}</p> @endifBlade directives make your templates easy to write, read, and maintain, so you can focus on building great features without messy code.
When building a blog, you can easily show or hide edit buttons only for logged-in authors using simple Blade directives, without cluttering your HTML with PHP.
Blade directives simplify mixing logic and HTML.
They improve code readability and reduce errors.
They help you build dynamic views quickly and cleanly.
Practice
Solution
Step 1: Understand Blade variable display
Blade uses double curly braces{{ }}to safely display variables with HTML escaping.Step 2: Identify correct directive for safe output
The{{ $variable }}syntax outputs the variable safely, preventing XSS attacks.Final Answer:
{{ $variable }} -> Option CQuick Check:
Safe variable display = {{ $variable }} [OK]
- Using @php for output instead of {{ }}
- Confusing @if with variable display
- Using @foreach for single variable output
$items?Solution
Step 1: Recall Blade loop directives
Blade uses@foreachto loop over arrays or collections.Step 2: Match correct syntax
The correct syntax is@foreach($items as $item), which loops through each item.Final Answer:
@foreach($items as $item) -> Option DQuick Check:
Loop over array = @foreach [OK]
- Using @loop which does not exist
- Using @for incorrectly for arrays
- Using @while for array loops
$count = 3?
@if($count > 5) Large @elseif($count > 1) Medium @else Small @endif
Solution
Step 1: Evaluate the condition
Since$count > 5$count = 3,3 > 5is false, so skip the first block.Step 2: Evaluate the
@elseif($count > 1)condition3 > 1is true, so Blade outputs "Medium" and skips the@else.Final Answer:
Medium -> Option BQuick Check:
3 > 1 but not > 5 = Medium [OK]
- Choosing Large because 3 is close to 5
- Ignoring @elseif and picking @else
- Thinking all blocks output
@foreach($users as $user){{ $user->name }}
@endforeach @endfor
Solution
Step 1: Check loop opening and closing directives
The loop starts with@foreachbut incorrectly ends with@endfor.Step 2: Match correct closing directive
Blade requires@endforeachto close a@foreachloop, so@endforis invalid here.Final Answer:
Incorrect closing directive @endfor instead of @endforeach -> Option AQuick Check:
Match @foreach with @endforeach [OK]
- Using @endfor to close @foreach
- Confusing $user and $users variables
- Missing closing directive entirely
header.blade.php inside your main view. Which directive correctly includes it and passes a variable $title with value 'Welcome'?Solution
Step 1: Recall correct syntax for @include with data
Blade's@includeaccepts the view name and an associative array of data.Step 2: Check array syntax for passing variables
The correct way is@include('header', ['title' => 'Welcome'])with quotes around string values.Final Answer:
@include('header', ['title' => 'Welcome']) -> Option AQuick Check:
Pass variables as array in @include [OK]
- Passing variable without array brackets
- Missing quotes around string values
- Using assignment syntax inside @include
Ready to prove it?
Pass the test and this pattern moves unknown
