0
0
Laravelframework~20 mins

Blade directives in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blade Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this Blade snippet?
Consider this Blade template code:
@if($user->isAdmin())
  

Admin Access

@else

User Access

@endif

If $user->isAdmin() returns false, what will be rendered?
Laravel
@if($user->isAdmin())
  <p>Admin Access</p>
@else
  <p>User Access</p>
@endif
A<p>Admin Access</p>
B<p>User Access</p>
CNo output
DSyntax error
Attempts:
2 left
💡 Hint
Think about what the @else directive does when the condition is false.
📝 Syntax
intermediate
1:30remaining
Which Blade directive correctly loops over an array?
You want to loop over $items in Blade. Which option uses the correct directive syntax?
A
@foreach($items as $item)
  {{ $item }}
@endforeach
B
@loop($items as $item)
  {{ $item }}
@endloop
C
@for($items as $item)
  {{ $item }}
@endfor
D
@repeat($items as $item)
  {{ $item }}
@endrepeat
Attempts:
2 left
💡 Hint
Blade uses a directive named after a common PHP loop for arrays.
🔧 Debug
advanced
2:00remaining
What error does this Blade code cause?
Given this Blade snippet:
@if($count > 0)
  

Count is positive

@else

No count

@endif

What error will occur when rendering?
Laravel
@if($count > 0)
  <p>Count is positive</p>
@else
  <p>No count</p>
@endif
ARuntime error: Undefined variable $count
BRenders both paragraphs
CNo output
DSyntax error: Unexpected @else without matching @if
Attempts:
2 left
💡 Hint
Check the placement of @else relative to @endif.
state_output
advanced
1:30remaining
What is the output of this Blade component with @isset?
Given $title = null; and this Blade code:
@isset($title)
  

{{ $title }}

@else

Default Title

@endisset

What will be rendered?
Laravel
@isset($title)
  <h1>{{ $title }}</h1>
@else
  <h1>Default Title</h1>
@endisset
A<h1>Default Title</h1>
B<h1></h1>
CNo output
D<h1>null</h1>
Attempts:
2 left
💡 Hint
Remember what @isset checks for in Blade.
🧠 Conceptual
expert
2:00remaining
Which Blade directive is used to include a child view and pass data?
You want to include a Blade view named header and pass ['title' => 'Welcome'] to it. Which directive and syntax is correct?
A@extends('header', ['title' => 'Welcome'])
B@component('header', ['title' => 'Welcome']) @endcomponent
C@include('header', ['title' => 'Welcome'])
D@yield('header', ['title' => 'Welcome'])
Attempts:
2 left
💡 Hint
Think about the directive that inserts a view and allows passing variables.