Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 482
Bert H Avatar asked May 25 '18 13:05

Bert H


People also ask

What is @show in Laravel?

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.

What is the advantage of Laravel blade template?

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.

What are the two primary benefits of Laravel blade?

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.

Is Laravel blade frontend?

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.

What is the difference between @stop and @endsection in Laravel?

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.

What is the difference between @endsection and @yield in Laravel?

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.

Is there a @stop in Laravel 5?

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.

What is the difference between @show and @section in Laravel?

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.


2 Answers

@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.

like image 101
apokryfos Avatar answered Oct 18 '22 21:10

apokryfos


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.

like image 8
Gursewak Singh Avatar answered Oct 18 '22 20:10

Gursewak Singh