I have main layout template /views/web/main_lo.blade.php
as
<html>
<head>
<meta charset="UTF-8">
<title>{{$title or 'Default Title'}}</title>
</head>
<body>
<div class="section-1-outer">
@section('section-1')
<div class="section-1-parent">parent section 1</div>
@show
</div>
<div class="section-2-outer">
@section('section-2')
<div class="section-2-parent">parent section 2</div>
@show
</div>
<div class="section-3-outer">
@section('section-3')
<div class="section-3-parent">parent section 3</div>
@show
</div>
<div>
@yield('content')
</div>
</body>
</html>
and a section template as:
@extends('web.main_lo')
@section('section-1')
@parent
<div class='section-1-child'>
<p>Appended to parent</p>
</div>
@stop
@section('section-2')
<div class='section-2-child'>
<p>Replace parent</p>
</div>
@stop
@section('section-3')
<div class='section-3-child'>
<p>Replace parent</p>
</div>
@overwrite
Now here section layout is extending main_lo, here First section-1 which is quite clear that child section will include parent section-1 and content in parent section will also be printed.
Now my confusion is what on earth is difference between section-2 and section-3 implementation as they both replace content of parent section and only content in child get printed. I mean what is need of this extra @overwrite
tag when documentation clearly states that
"Note that views which extend a Blade layout simply override sections from the layout."
and then there is Overwriting Sections using @overwrite
which is also for replacing content of parent section.
The blade templates are stored in the /resources/view directory. The main advantage of using the blade template is that we can create the master template, which can be extended by other files.
You could download the class and start using it, or you could install via composer. It's 100% compatible without the Laravel's own features (extensions).
Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .
Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.
Laravel view's sections are a bit oddish and the bad part is that it's not obvious from the start.
Sections are actually extending in converse order, the section defined first will be the child and the one later will be the parent. So @parent
is actually including the content of the section which may come after.
This is why I think parent and child are not the best terms for this system.
This is not obvious because of the conventional use case - layouts - it looks like the layout sections are the ones defined first. But actually they ran after the content sections are rendered.
This means @overwrite
is actually used with the parent section, not the child. Which wouldn't make much sense in your example as you're yielding (@show
) in the layout too, meaning you don't want to overwrite there.
The reason for this is actually because @overwrite
was made for less conventional use cases, not when you have a layout-content, parent-child relationship.
Most often this happens when you include some partial files - maybe all over the place - where you use the same section names and you run into a problem when the earlier defined section is the one showing instead of the later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With