Blade 查看 'has stack' 条件

Blade view 'has stack' condition

我想检查 parent 布局是否有 children 推送内容。

与我用 View::hasSection('section-name')hasSection 方法检查的方式相同。我正在寻找类似的东西:hasStack.

目的是这样的:

@if(/* has stack 'foo' */)
    <div id='foo'>
        @stack('foo')
    </div>
@endif

最好的方法是什么?

[注意]我正在处理 Laravel 5.4

我正在使用 Laravel 7 和以下工作来检查推送的内容:

@if($__env->yieldPushContent('foo'))
   
    <p>There is something in the "foo" stack.</p>

@endif

您可以将其放入 custom blade directive:

    Blade::if('hasStack', function($stackName) {
        return !empty(view()->yieldPushContent($stackName));
    });

并像这样使用它:

@hasStack('foo')

    <p>There is something in the "foo" stack.</p>

@endhasStack

请注意 - @end 部分不对指令名称进行驼峰命名,因此它是@endhasStack,而不是@endHasStack。