Default values ββfor Laravel 5.4+ new components?
Laravel 5.4+ allows you to use components with the following structure:
<div class="alert alert-{{ $type }}">
<div class="alert-title">{{ $title }}</div>
{{ $slot }}
</div>
Called as:
@component('alert', ['type' => 'danger'])
@slot('title')
oh no
@endslot
Foo
@endcomponent
Is there a blade pointer or handle to set defaults for the variables passed in?
For example, in the case above, if I don't pass "type", I get an error undefined variable
. I could do something like:
<div class="alert alert-{{ isset($type) ? $type : 'default' }}">
But the above looks verbose, especially if the variable is used at multiple points.
+3
source to share
1 answer
I don't think it is.
Having said that, the blade has an operator or
that wraps the call isset()
( see docs ).
<div class="alert alert-{{ $type or 'default' }}">
If you are using php 7 this is the case for the Null coalescing operator .
<div class="alert alert-{{ $type ?? 'default' }}">
Both will make your site callable.
+8
source to share