Passing variable from component into a slot
We can try with different way. Here, how I executed.
{{-- index.blade.php --}}
@component('slider', ['entities' => [0, 1, 2]])
@slot('title')
Slider title
@endslot
@slot('slide')
Slider content no
@endslot
@endcomponent
{{-- slider.blade.php --}}
<h1>{{ $title }}</h1>
<ul>
@foreach($entities as $entity)
<li>{{ $slide }} {{ $entity }}</li>
@endforeach
</ul>
It seems that there is no way to pass data from a component to a slot context. It is also the case with @section
/@yield
.
What I've discovered is the @each
function. https://laravel.com/docs/5.6/blade#rendering-views-for-collections
You'd need to have another view partial for the list item content (called item
here).
{{-- index.blade.php --}}
@component('slider', ['entities' => [0, 1, 2], 'item_view' => 'item'])
@slot('title')
Slider title
@endslot
@endcomponent
{{-- item.blade.php --}}
<li>
Slider content no $entity
</li>
{{-- slider.blade.php --}}
<h1>{{ $title }}</h1>
<ul>
@each($item_view, $entities, 'entity')
</ul>
Example: making a new slider with different content:
{{-- gallery.blade.php --}}
@component('slider', ['entities' => ['a.png', 'b.png', 'c.png'], 'item_view' => 'gallery_item'])
@slot('title')
Gallery
@endslot
@endcomponent
{{-- gallery_item.blade.php --}}
<li>
<img src={{ $entity }} />
</li>