Laravel Section vs Stalk

In Laravel we use section to define some HTML and then yield it somewhere else in the code. It appears that section and stack has the same purpose but they have difference both logically and depending on the behaviour as well. You can append stack as many times as needed with @push. You can only use @section once in your views. You can use them in js and css as well. When you need to add contents from different locations in your template files or in loops it is useful.

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>


Output

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