检查 laravel Blade 组件中的 $slot 是否为空

Asa*_*ood 7 conditional-statements laravel laravel-blade

我有一个输出 $slot 的组件。但我想做一些类似的事情:

成分

<x-alert></x-alert>
Run Code Online (Sandbox Code Playgroud)
<div>
@if ($slot)
    {{ $slot }}
@else
    No available content
@endif
</div>
Run Code Online (Sandbox Code Playgroud)

小智 23

$slot是该类的一个实例Illuminate\Support\HtmlString。所以你可以使用以下isNotEmpty方法:

<div>
@if ($slot->isNotEmpty())
    {{ $slot }}
@else
    No available content
@endif
</div>
Run Code Online (Sandbox Code Playgroud)


Mou*_*isy 3

Laravel 组件中的变量$slot被准备为 Object 类型Illuminate\Support\HtmlString
,因此需要像这样检查:

<div>
@if(strlen($slot->toHtml()) != 0)
    {{$slot}}
@else
    slot is empty
@endif
Run Code Online (Sandbox Code Playgroud)