How to pass an array to @slot in laravel 5.4?

I'm trying to use it in my layout and I suddenly come up with the idea of ​​using the new features of Laravel 5.4 @slot.

Just wondering, is it possible to pass an array to a slot?

        @section('SampleSection')

            @component( 'mylayouts.partials.contentheader' )

                @slot('title')
                    Sample Title
                @endslot

                @slot('indexes')
                    Pass array here  // example [ 'a', 'b', 'c' ]
                @endslot

            @endcomponent

        @endsection
+4
source share
4 answers

I need the same thing.

This works as a temporary solution, but is not the best form for this.

@component('component.tab.header')
    @slot('navs', [
        "a" => "Pepe",
        "b" => "Maria"
    ])
@endcomponent

EDIT:

Finally, in my case, I decided to do this:

@component('component-name')
    @slot('slot-name')
        Value1|Value2|Value2
    @endslot
@endcomponent

And in the component code:

@foreach(explode('|', $slot-name) as $value)
    The value is {{ $value }} <br/>
@endforeach
+4
source

Maybe I'm wrong, but could you just pass the data to the component and process it in the component? Documents show

. @component. :

@component ('alert', ['foo' = > 'bar'])   ... @endcomponent

?

+1

- @slot

view.blade.php

@component('components.button',[
  'tooltipTitle'=>'buttorr33',
  'addStyle'=>'pointer',
  ])
  @endcomponent

php file\button.php

<div class="block-buttons {{$addStyle or NULL}}"
     {{--if set tooltip, add atributes--}}
     @if (isset($tooltipTitle))...
+1

, @slot. , , .

:

Sometimes you may need to pass additional data to a component. For for this reason, you can pass an array of data as the second argument to the @component directive. All data will be available for the component template as variables:

@component('alert', ['foo' => 'bar'])
    ...
@endcomponent

So let's say you have a data array:

$my_array = ['a', 'b', 'c']

You can pass it to a component like this:

@component('mylayouts.partials.contentheader', ['my_array' => $my_array])
    ...
@endcomponent

Or you can just pass it directly without using a variable:

@component('mylayouts.partials.contentheader', ['my_array' => ['a', 'b', 'c']])
    ...
@endcomponent

Then, in your component file, you will access the variable $my_array. For example, in a file, you can do this: contentheader.blade.php

<ul>
    @foreach($my_array as $value)
        <li>{{ $value }}</li>
    @endforeach
</ul>
+1
source

Source: https://habr.com/ru/post/1668095/


All Articles