如何清除blade.php中定义的部分

问题描述

我知道 Laravel-blade allows us to define sections of code,但我想知道是否可以清除或重新定义同一文件中定义它的部分。

例如,类似于:

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


//something like
@section('scripts')
    // nothing
@endsection



// Now the 'scripts' stack is an empty block of code.


 //print
 {{  @yield('scripts')  }}

解决方法

我已经找到了一种方法,只需在常规 php 中使用 ob_start()

        @php
            ob_start();
        @endphp
            // write blade code here
        @php
            $scripts = ob_get_contents();
            ob_end_clean();
        @endphp
        
          //print
         {!! $scripts !!}