Laravel Blade: @stop VS @show VS @endsection VS @append

@endsection and @stop are the same and indicate the end of a section.

The section is not actually rendered on the page until you do @yield('sectionname')

In contrast, @show is equivalent to

@stop
@yield('sectionname')

i.e. it stops and immediately renders the section at that part of the page.

@append is basically equivalent to:

//FileA.blade.php
@section('sectionname')
 ... content
@stop

//FileB.blade.php
@extends('fileA')

@section('sectionname')
    @parent
    ... more content after content
@stop

Here's some relevant source code:

protected function compileStop() {
    return '<?php $__env->stopSection(); ?>';
}
protected function compileEndsection() {
    return '<?php $__env->stopSection(); ?>'; //Same code
}

protected function compileShow() {
    return '<?php echo $__env->yieldSection(); ?>';
}

Yield section just stops the current section and yields its contents.


I might be late in the party. But in Laravel 7.x series, there is no mention of "@stop" and "@append".

Q: Difference between @endsection and @show

@endsection directive just tells the blade engine where the section actually ends. And to show that section you need to use the @yield directive. If you don't yield that section blade won't show it after rendering the view.

For example in layout view:

<!-- layout -->
<body>

    @section('content')
      Some Content
    @endsection

</body>

The above code has no meaning, of course we have defined a section. But it won't show up in the view to the client. So we need to yield it, to make it visible on the client. So lets yield it in the same layout.

<!--layout-->
<body>

    @section('content')
        Some content
    @endsection
    @yield('content')

</body>

Now the above code has some meaning to the client, because we have defined a section and told the Blade engine to yield it in the same layout.

But Laravel provides a shortcut to that, instead of explicitly yielding that section in the same layout, use the @section - @show directive pairs. Therefore the above code can be written as follows:

<!--layout-->
<body>

    @section('content')
        Some content
    @show

</body>

So @show is just a compacted version of @endsection and @yield directives.

I hope I made everything clear. And one more thing, in laravel 7.x to append the content in the section, @parent directive is used.