How to pass an array to a @slot in laravel 5.4?
Asked Answered
M

7

9

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

Just wondering if passing an array to a slot possible?

        @section('SampleSection')

            @component( 'mylayouts.partials.contentheader' )

                @slot('title')
                    Sample Title
                @endslot

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

            @endcomponent

        @endsection
Michaeline answered 28/1, 2017 at 11:17 Comment(0)
P
11

As far as I know, it isn't possible to pass an array to a component via @slot. However, it is possible to pass and an array to a component directly, without using a slot.

From the documentation:

Passing Additional Data To Components

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

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

So let's say you have an array of data:

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

You can pass it to the component like so:

@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, within your component file, you will have access to a $my_array variable. For example, in your contentheader.blade.php file you can do this:

<ul>
    @foreach($my_array as $value)
        <li>{{ $value }}</li>
    @endforeach
</ul>
Pearson answered 6/3, 2018 at 16:59 Comment(0)
N
8

I need the same.

This works as a temporally solution, but is not the best form for do it.

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

EDIT:

Finally, in my case, I solved doing 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
Noland answered 28/1, 2017 at 16:26 Comment(4)
Actually tried that one but was having difficulty catching the array in the slot views. What I did is put another '@include' inside the '@component' and using a variable parameters for the slot to work. It's not pretty though...Michaeline
See my new edit, I thinks this is the more clear-code option.Noland
I think your original answer is actually the better one. I'm going to try and ask this week in the Slack to see what the intended way is, and hopefully update the documentation after so it is clearer.Alisander
@Ivan - that's precisely what I did in my previous code but I ended up using @include instead. I just hope Taylor would add functionality to add variable parameters in @slot instead of doing what we just did or there might be better ways to do it.Michaeline
H
6

Here's an example component. Set the number of items to the $numberOfSlots variable and it will search for slot blocks one by one (body_1, body_2, body_3).

Since I'm needed a form wizard and it's not aesthetically pleasing to define HTML blocks between " ", I prefer using @slot.

Component:

<div class="component">
    @for($i = 1; $i <= $numberOfSlots; $i++)
        <div class="item">
            {{ ${"body_{$i}"} }}
        </div>
    @endfor
</div>

Blade:

@component(['numberOfSlots' => 3])
    @slot('body_1')
            Dynamically created slot 1
    @endslot

    @slot('body_2')
            Dynamically created slot 2
    @endslot

    @slot('body_3')
            Dynamically created slot 3
    @endslot
@endcomponent
Hakluyt answered 11/4, 2019 at 11:33 Comment(1)
Works fine so far, specially for listsExtensile
G
1

I may be wrong but couldn't you just pass the data to the component and handle that in the component? Docs show

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

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

deal with the data in the component instead of using a slot?

Griffie answered 23/3, 2017 at 18:21 Comment(0)
R
1

it's so simple and easy send array into component template without any @slot in view page

view.blade.php

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

in component php file component\button.php

<div class="block-buttons {{$addStyle or NULL}}"
     {{--if set tooltip, add atributes--}}
     @if (isset($tooltipTitle))...
Rubirubia answered 6/6, 2017 at 13:0 Comment(1)
@Nelson Melicio sending data into view via array, in view must be variable with the same name like key in associative array. 'toolitipTitle'=>'buttor33' in view file will send it into component which is set at first param. Then in that component file you could use variable with name $tooltipTitle. If it doesn't set it will throw an errorRubirubia
H
1

How I do it:

Include @PageNav component tag in any view

@PageNav([
    'menus' => [
        [
            'title' => 'foo', 
            'url' => '/foo/index'
        ], 
        [
            'title' => 'bar', 
            'url' => '/bar/page'
        ]
    ]
]) @endPageNav

Create view/components/PageNav.php file

@foreach ($menus as $menu)
  <li class="nav-item">
    <a class="nav-link" href="{{ $menu['url'] }}">{{ $menu['title'] }}</a>
  </li>
@endforeach

Register the component: (AppServiceProvider.php -> Boot)

Blade::component('components.PageNav'); // "/views/components/PageNav.php"

Hope it's help!

Hausfrau answered 19/7, 2018 at 15:39 Comment(0)
K
0

Laravel 10 <-- current latest version

https://laravel.com/docs/10.x/blade#passing-data-to-components

<x-mylayouts.partials.contentheader :title="$title" :indexes="$my_array" />
  • trailing / means no matching closing tag needed
  • this will load /resources/views/components/mylayouts/partials/contentheader.blade.php
Kavanaugh answered 28/2, 2023 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.