首页 > 解决方案 > 如何清除blade.php中定义的部分

问题描述

我知道Laravel-blade 允许我们定义代码段,但我想知道是否可以清除或重新定义与定义相同的文件中的部分。

例如,类似:

@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')  }}

标签: phplaravel-blade

解决方案


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

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

推荐阅读