In Laravel, there are different ways of using sections:
@yield('section1') // Filled in by child view
@section('section2')
// Default content, can be overwritten by child
// Different ways of closing the section
@endsection|stop|show|append
Who can tell me what the exact difference is between all of these?
Accoding to this, @stop and @endsection might be the same. (with one having been deprecated, but not anymore)
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. Follow this answer to receive notifications.
In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.
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.
You've also learned the basics of Laravel blade that you need to be able to collaborate with a team of Laravel Devs as a frontend Dev. Be sure to visit the Laravel docs to study more about the blade templates and how you can use Layouts to minimize unnecessary repetitions.
But in Laravel 7.x series, there is no mention of "@stop" and "@append". @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.
But in Laravel 7.x series, there is no mention of "@stop" and "@append". @endsection directive just tells the blade engine where the section actually ends. And to show that section you need to use the @yield directive.
But from Laravel 5 onwards @stop is not even mention in the documentation 2. So I would suggest to use @endsection. Show activity on this post. @endsection became @stop in L4, just as @yieldSection became @show.
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: So @show is just a compacted version of @endsection and @yield directives.
@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.
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