Problem
You want to merge in the content of a parent section in your Blade template.
Solution
Use the Blade command.
This will pull in the content of the previous section of the same name.
For example.
@section('test')
Bottom
@stop
{{-- Later --}}
@section('test')
Top
@show
This would output the following.
Top Bottom
Discussion
At first glance the above seems to get cause and effect backwards.
But it doesn't. The logic working through that code is:
- Line 1
-
Start capturing output. The section will be called
test. - Line 2 - 3
-
Capture the two lines
andBottom. - Line 4
-
Stop capturing output. There's no previous
testsection, so all we do is store these two lines. - Line 7
-
Start capturing output. The section will be called
test. - Line 8
-
Capture the line
Top - Line 9
-
Stop capturing data. Since there's already a section named
test, we'll replace anyin the previous section with our captured data. Now ourtestsection contains two lines:TopandBottom. Since this was a@showcommand we now yield thetestsection, outputting these two lines.
When you use an @extend at the top of a Blade template, Blade will first load and process your existing template, then it will load and process the template specified with the @extend command.
If you think about it. How the @extend command and the command work, compliment each other nicely.
