gpt4 book ai didi

php - Laravel 5.4+ blades 新组件的默认值?

转载 作者:可可西里 更新时间:2023-10-31 22:43:56 25 4
gpt4 key购买 nike

Laravel 5.4+ 允许使用以下结构的组件:

<div class="alert alert-{{ $type }}">
<div class="alert-title">{{ $title }}</div>
{{ $slot }}
</div>

它们被称为:

@component('alert', ['type' => 'danger'])
@slot('title')
oh no
@endslot
Foo
@endcomponent

是否有速记或 Blade 标记来为传入的变量设置默认值?

例如,在上面,如果我不传入“type”,我会得到一个undefined variable错误。我可以做类似的事情:

<div class="alert alert-{{ isset($type) ? $type : 'default' }}">

但是上面的内容感觉很冗长,尤其是当变量在多个位置使用时。

最佳答案

我不这么认为。

话虽如此,blade 具有包装 isset() 调用 ( see docs ) 的 or 运算符。

<div class="alert alert-{{ $type or 'default' }}">

如果您运行的是 php 7,这就是 Null coalescing operator 的情况.

<div class="alert alert-{{ $type ?? 'default' }}">

两者都会使您的调用站点更整洁。

关于php - Laravel 5.4+ blades 新组件的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42966549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com