8

我试图在我的布局中使用它,我突然想到使用 Laravel 5.4 @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

6 回答 6

11

据我所知,不可能通过@slot. 但是,可以直接将数组传递给组件,而无需使用插槽。

文档中:

将附加数据传递给组件

有时您可能需要将其他数据传递给组件。因此,您可以将数据数组作为第二个参数传递给@component 指令。所有数据都将作为变量提供给组件模板:

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

所以假设你有一个数据数组:

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

您可以像这样将它传递给组件:

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

或者您可以直接传递它,而不使用变量:

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

然后,在您的组件文件中,您将可以访问一个$my_array变量。例如,在您的contentheader.blade.php文件中,您可以这样做:

<ul>
    @foreach($my_array as $value)
        <li>{{ $value }}</li>
    @endforeach
</ul>
于 2018-03-06T16:59:39.297 回答
8

我需要同样的。

这可以作为一种临时解决方案,但不是最好的形式。

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

编辑:

最后,就我而言,我解决了这个问题:

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

在组件代码中:

@foreach(explode('|', $slot-name) as $value)
    The value is {{ $value }} <br/>
@endforeach
于 2017-01-28T16:26:14.767 回答
4

这是一个示例组件。将项目数分配给$numberOfSlots变量,它将一一搜索槽块(body_1, body_2, body_3

因为我构建了一个表单向导,并且在它们之间定义 html 块太难看," "所以我更喜欢@slot.

零件:

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

刀:

@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
于 2019-04-11T11:33:18.933 回答
1

将数组发送到组件模板中是如此简单易行,而视图页面中没有任何@slot

view.blade.php

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

在组件 php 文件component\button.php

<div class="block-buttons {{$addStyle or NULL}}"
     {{--if set tooltip, add atributes--}}
     @if (isset($tooltipTitle))...
于 2017-06-06T13:00:00.993 回答
1

我可能错了,但您不能将数据传递给组件并在组件中处理吗?文档显示

有时您可能需要将其他数据传递给组件。因此,您可以将数据数组作为第二个参数传递给@component 指令。所有数据都将作为变量提供给组件模板:

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

处理组件中的数据而不是使用插槽?

于 2017-03-23T18:21:17.790 回答
1

我是怎么做的:

在任何视图中包含 @PageNav 组件标记

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

创建视图/组件/PageNav.php 文件

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

注册组件:(AppServiceProvider.php -> Boot)

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

希望有帮助!

于 2018-07-19T15:39:16.343 回答