What is the difference between Section and Stack in Blade?

前端 未结 3 1854
忘了有多久
忘了有多久 2021-02-03 20:50

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

3条回答
  •  一个人的身影
    2021-02-03 21:35

    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')
        
      @endpush
    
      @section('test-section')
        
      @endsection
    
    @endfor
    

    master.blade.php

        @stack('test-push')
        @yield('test-section')
    
    

    result:

        
            
            
        
        
    

提交回复
热议问题