What is the difference between Section and Stack in Blade?

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>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)