Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cbj4074's avatar

Trouble with Blade section inheritance

Hello!

Blade template inheritance seems so straightforward, yet when I cobble-together the most basic scenario, inheritance doesn't behave the way I had expected.

Take, for example, these three Blade templates:

template-1.blade.php

@section('myMarkup')
    DEFAULT markup goes here.
@endsection

@yield('myMarkup')

template-2.blade.php

@extends('template-1')

@section('myMarkup')
    OVERRIDE markup goes here.
@endsection

template-3.blade.php

<div>
    @include('template-1')
    @include('template-2')
</div>

Route:

Route::get('/test', function () \\{
    return view('template-3');
});

Requesting this route produces the following output:

<div>
    DEFAULT markup goes here.
    DEFAULT markup goes here.
</div>

I had expected the following instead:

<div>
    DEFAULT markup goes here.
    OVERRIDE markup goes here.
</div>

What might I be missing here?

Thanks for any help!

0 likes
2 replies
cbj4074's avatar

Thank you, @poppabear (who helped me through a side channel)!

I should have known to go back to the Laravel 4.2 documentation, as it explains the @overwrite directive, which is exactly what I was missing:

template-2.blade.php

    @extends('template-1')

    @section('myMarkup')
        OVERRIDE markup goes here.
    @overwrite

This produces the following, as expected:

    <div>
        DEFAULT markup goes here.
        OVERRIDE markup goes here.
    </div>

For whatever reason, this information was expunged from the documentation between versions 5.0 and 5.1.

This is a recurring pattern in the Laravel documentation. Crucial information is expunged between versions, and repeated attempts to have it re-added (i.e., PRs on GitHub) are ignored. Extremely frustrating.

And, aside from that, the current documentation is flatly incorrect in that it states the following at the bottom of https://laravel.com/docs/master/blade#extending-a-layout :

In this example, the sidebar section is utilizing the @parent directive to append (rather than overwriting) content to the layout's sidebar.

The implication is that the default behavior, when extending a template and defining a section with the same name as a parent template, is to overwrite the section completely. That assertion is provably untrue, as evidenced above.

1 like

Please or to participate in this conversation.