Performance: Blade directives
Blade directives affect server-side rendering speed and the size of the generated HTML, impacting initial page load time.
@if($user->isAdmin()) <div>Admin Panel</div> @endif
@php if($user->isAdmin()) { echo '<div>Admin Panel</div>'; } @endphp
| Pattern | Server CPU Cost | HTML Size | Network Impact | Verdict |
|---|---|---|---|---|
| Raw PHP in Blade | High | Potentially Large | Slower Transfer | [X] Bad |
| Proper Blade directives | Low | Minimal | Faster Transfer | [OK] Good |
| Large loops without limits | High | Very Large | Slow Transfer | [X] Bad |
| Limited loops with pagination | Low | Small | Fast Transfer | [OK] Good |
{{ }} to safely display variables with HTML escaping.{{ $variable }} syntax outputs the variable safely, preventing XSS attacks.$items?@foreach to loop over arrays or collections.@foreach($items as $item), which loops through each item.$count = 3?
@if($count > 5) Large @elseif($count > 1) Medium @else Small @endif
$count > 5$count = 3, 3 > 5 is false, so skip the first block.@elseif($count > 1) condition3 > 1 is true, so Blade outputs "Medium" and skips the @else.@foreach($users as $user){{ $user->name }}
@endforeach @endfor
@foreach but incorrectly ends with @endfor.@endforeach to close a @foreach loop, so @endfor is invalid here.header.blade.php inside your main view. Which directive correctly includes it and passes a variable $title with value 'Welcome'?@include accepts the view name and an associative array of data.@include('header', ['title' => 'Welcome']) with quotes around string values.Pass the test and this pattern moves unknown