0
0
Laravelframework~10 mins

Including sub-views (@include) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Including sub-views (@include)
Main View Starts Rendering
@include Directive Found
Load Sub-view Template
Render Sub-view Content
Insert Sub-view Content into Main View
Continue Main View Rendering
Main View Fully Rendered
The main view starts rendering, finds an @include directive, loads and renders the sub-view, inserts its content, then continues rendering the main view.
Execution Sample
Laravel
@include('header')
<p>Welcome to the site!</p>
@include('footer')
This code includes the 'header' sub-view, then shows a welcome message, then includes the 'footer' sub-view.
Execution Table
StepActionTemplate ProcessedOutput ProducedNotes
1Start rendering main viewmain viewBegin main template processing
2Encounter @include('header')main viewPause main view to load sub-view
3Load and render 'header' sub-viewheader<header>Site Header</header>Sub-view content generated
4Insert 'header' content into main viewmain view<header>Site Header</header>Sub-view content placed
5Render main view content after headermain view<p>Welcome to the site!</p>Main content rendered
6Encounter @include('footer')main viewPause main view to load sub-view
7Load and render 'footer' sub-viewfooter<footer>Site Footer</footer>Sub-view content generated
8Insert 'footer' content into main viewmain view<footer>Site Footer</footer>Sub-view content placed
9Finish rendering main viewmain view<header>Site Header</header><p>Welcome to the site!</p><footer>Site Footer</footer>Full page output ready
💡 All parts rendered and combined, main view output complete
Variable Tracker
VariableStartAfter Step 3After Step 7Final
main_view_output"""<header>Site Header</header>""<header>Site Header</header><p>Welcome to the site!</p>""<header>Site Header</header><p>Welcome to the site!</p><footer>Site Footer</footer>"
sub_view_content"""<header>Site Header</header>""<footer>Site Footer</footer>"""
Key Moments - 2 Insights
Why does the main view pause rendering when it hits @include?
Because @include tells Laravel to load and render the sub-view first, then insert its content before continuing the main view. See steps 2-4 and 6-8 in the execution table.
Is the sub-view output separate or combined with the main view?
The sub-view output is generated separately but immediately inserted into the main view output. This is shown by the 'sub_view_content' variable in the tracker and the combined output in the main_view_output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 4?
A"<footer>Site Footer</footer>"
B"<header>Site Header</header>"
C"<p>Welcome to the site!</p>"
D""
💡 Hint
Check the 'Output Produced' column at step 4 in the execution table.
At which step does the footer sub-view content get inserted into the main view?
AStep 8
BStep 5
CStep 3
DStep 9
💡 Hint
Look for the step where 'Insert 'footer' content into main view' happens in the execution table.
If the 'header' sub-view was empty, how would main_view_output change after step 4?
A""
B"<p>Welcome to the site!</p>"
C"" (empty string)
D"<header>Site Header</header>"
💡 Hint
Refer to variable_tracker for 'main_view_output' after step 3 and imagine 'sub_view_content' is empty.
Concept Snapshot
@include('view_name') inserts another view's content into the current view.
Laravel pauses the main view rendering, loads and renders the sub-view,
then inserts its output right where @include is called.
This helps reuse common parts like headers or footers.
Sub-views render fully before main view continues.
Full Transcript
When Laravel renders a view with @include, it starts the main view rendering. When it finds @include('subview'), it pauses the main view, loads and renders the sub-view fully, then inserts that sub-view's content into the main view output. After insertion, the main view continues rendering. This process repeats for each @include. The final output combines all parts into one HTML page. Variables track the main view output growing as sub-views insert their content. This lets developers reuse pieces like headers or footers easily by including them where needed.