Challenge - 5 Problems
Blade Include Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What will be rendered by this Blade template?
Given the main view includes a sub-view with
@include('subview', ['name' => 'Alice']), and the sub-view contains Hello, {{ $name }}!, what is the output when rendering the main view?Laravel
@include('subview', ['name' => 'Alice']) // subview.blade.php content: // Hello, {{ $name }}!
Attempts:
2 left
💡 Hint
Remember that variables passed in the second argument of @include are available inside the sub-view.
✗ Incorrect
The @include directive passes the array ['name' => 'Alice'] to the sub-view, so $name is defined as 'Alice'. The sub-view renders 'Hello, Alice!'.
📝 Syntax
intermediate1:30remaining
Which @include syntax correctly passes multiple variables to a sub-view?
You want to include a sub-view and pass variables
$title = 'Welcome' and $count = 5. Which option is correct?Attempts:
2 left
💡 Hint
Check the correct syntax for passing an associative array as the second argument.
✗ Incorrect
The @include directive expects the second argument as an associative array mapping variable names to values. Option B uses the correct array syntax.
🔧 Debug
advanced2:00remaining
Why does this @include cause an error?
This Blade code causes an error:
But the variable
@include('footer', ['year' => $year])But the variable
$year is defined only in the parent view inside a @section('content'). Why?Laravel
@section('content') @include('footer', ['year' => $year]) @php $year = 2024; @endphp @endsection
Attempts:
2 left
💡 Hint
Think about variable scope inside Blade sections and when @include is processed.
✗ Incorrect
Variables defined inside a @section block are not available outside it at the time @include runs, causing $year to be undefined in the include call.
❓ component_behavior
advanced1:30remaining
What happens if the included sub-view does not exist?
Consider this Blade code:
What will Laravel do when rendering this view?
@include('nonexistent')What will Laravel do when rendering this view?
Laravel
@include('nonexistent')Attempts:
2 left
💡 Hint
Think about how Laravel handles missing views during rendering.
✗ Incorrect
If the included view file does not exist, Laravel throws a clear error indicating the missing view.
❓ state_output
expert2:30remaining
What is the output of this nested @include with variable shadowing?
Given these Blade files:
What is the full rendered output of
// main.blade.php
@php $color = 'red'; @endphp
@include('subview', ['color' => 'blue'])
// subview.blade.php
Color: {{ $color }}
@include('subsubview')
// subsubview.blade.php
Color again: {{ $color }}What is the full rendered output of
main.blade.php?Laravel
// main.blade.php @php $color = 'red'; @endphp @include('subview', ['color' => 'blue']) // subview.blade.php Color: {{ $color }}<br> @include('subsubview') // subsubview.blade.php Color again: {{ $color }}
Attempts:
2 left
💡 Hint
Consider how variables passed to @include affect nested includes and variable scope.
✗ Incorrect
The variable $color is passed as 'blue' to subview. Inside subview, $color is 'blue'. The nested @include('subsubview') inherits the current variable scope, so $color remains 'blue' there too.