Laravel 5.4+刀片新组件的默认值是多少?

Chr*_*ris 10 php laravel laravel-5 laravel-5.4

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

<div class="alert alert-{{ $type }}">
    <div class="alert-title">{{ $title }}</div>
    {{ $slot }}
</div>
Run Code Online (Sandbox Code Playgroud)

这被称为:

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

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

例如,在上面,如果我没有传入"类型",我会收到undefined variable错误.我可以这样做:

<div class="alert alert-{{ isset($type) ? $type : 'default' }}">
Run Code Online (Sandbox Code Playgroud)

但上述情况很冗长,特别是如果变量用于多个点.

Jes*_*edc 16

我不这么认为.

说过刀片有or操作员打包isset()电话(参见文档).

<div class="alert alert-{{ $type or 'default' }}">
Run Code Online (Sandbox Code Playgroud)

如果你正在运行php 7,这就是Null合并运算符的情况.

<div class="alert alert-{{ $type ?? 'default' }}">
Run Code Online (Sandbox Code Playgroud)

两者都会使你的呼叫网站tider.