Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Section and Stack in Blade?

We can use a section to define some HTML and then yield that somewhere else.

So why do we have stacks? https://laravel.com/docs/5.2/blade#stacks

It's doing exactly the same thing with different keywords, but has fewer options (No inheritance).

@push('scripts')
    <script src="/example.js"></script>
@endpush

<head>
    <!-- Head Contents -->

    @stack('scripts')
</head>

Can be done with section:

@section('scripts')
    <script src="/example.js"></script>
@endsection

<head>
    <!-- Head Contents -->

    @yield('scripts')
</head>
like image 616
Positivity Avatar asked Feb 27 '16 16:02

Positivity


People also ask

What is a blade stack?

The stacked dado blade is a series of blades in a stack that create a wider blade. These blades can be interspersed with spacers to achieve a precise cut. Outside of the outer blades are chipper blades that can be added or removed, depending on your cut width. The chippers also determine the length of the cut.

What is Section in Laravel?

@section directive is inject content layout from extended blade layout and display in child blade. The content of these section will be displayed in the layout using @yield directive. @parent directive will be replaced by the content of the layout when the view is rendered.

What is Laravel stack?

It is a web application framework with expressive, elegant syntax. It attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching. Laravel is a tool in the Frameworks (Full Stack) category of a tech stack.

What is the use of @yield in Laravel?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.


3 Answers

I might be mistaken, but the difference is not only semantically, but in behaviour as well. With @push you append as many times as needed to a stack, while (by default) you may fill @section only once in your views. In some situations this comes in handy when you need to add content from different locations across your template files or in loops:

index.blade.php:

@extends('master')

...  

@for ($i = 0; $i < 3; $i++)

  @push('test-push')
    <script type="text/javascript">
    // Push {{ $i }}
    </script>
  @endpush

  @section('test-section')
    <script type="text/javascript">
    // Section {{ $i }}
    </script>
  @endsection

@endfor

master.blade.php

    @stack('test-push')
    @yield('test-section')
</body>

result:

    <script type="text/javascript">
    // Push 0
    </script>
        <script type="text/javascript">
    // Push 1
    </script>
        <script type="text/javascript">
    // Push 2
    </script>
    <script type="text/javascript">
    // Section 0
    </script>
    </body>
like image 199
macghriogair Avatar answered Oct 23 '22 14:10

macghriogair


Stack is someway appropriate for scripts , with stack you can Append as much as you need .

@push('scripts')
    <script src="/example.js"></script>
 @endpush

Append …

<script>
@stack('scripts')
</script>

As you can see the script's stack will be appended under the script tag of example js. So you can push special scripts for each view.

like image 45
mercury Avatar answered Oct 23 '22 14:10

mercury


@section - You can add your js css once.
@stack   - Push js css into stack (page) many times.
like image 2
Vinay Kaithwas Avatar answered Oct 23 '22 12:10

Vinay Kaithwas