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/