Laravel Blade: @stop VS @show VS @endsection VS @append
Asked Answered
F

3

16

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)

Fractionate answered 25/5, 2018 at 13:24 Comment(0)
H
19

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

Homesteader answered 25/5, 2018 at 13:29 Comment(0)
B
14

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.

Brilliantine answered 8/9, 2020 at 6:23 Comment(0)
E
0

Found it in quora

@show has similar functionality like @yield but you can use it for more things. When you declare @yield in your layout view you can set default value as well.

app.blade.php
@yield('title', 'Default Title') 

and in your child view you can change the title

user.blade.php

@section('title', 'Custom Title') 

However, you can set only string for default value in case of @yield. If you want to use some part of html content as a default part, you should use @show

app.blade.php

@section('some_div') 
    <h1>Default Heading1</h1> 
    <p>Default Paragraph 
        <span>Default Span</span> 
    </p> 
@show

in your child view

@section('some_div') 
    <h1>Custom Heading1</h1> 
    <p>Custom Paragraph 
        <span>Custom Span</span> 
    </p> 
@endsection 

also you can use both default content and custom content together. just include @parent

@section('some_div') 
@parent 
    <h1>Custom Heading1</h1> 
    <p>Custom Paragraph 
        <span>Custom Span</span> 
    </p> 
@endsection
Elasmobranch answered 12/10, 2021 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.