php - 如何将数组传递给 laravel 5.4 中的@slot?

标签 php laravel-blade laravel-5.4

我正尝试在我的布局中使用它,我突然想到使用 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

最佳答案

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

来自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

假设您有一组数据:

$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>

关于php - 如何将数组传递给 laravel 5.4 中的@slot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909142/

相关文章:

javascript - 删除 Javascript 对象的 foreach 循环内的引号 - Laravel 5.4 - PHP

php - 在sql中选择查询

php - 如何从提交按钮调用 php 函数?

php - 如何记录用户点击的内容

php - 水平缩放 : routing user-generated subdomains between servers

mysql - 如何在 Laravel 5.4 中根据用户 ID 排除某些行?

laravel - 不能在 Blade 模板中使用 OR

php - Laravel Blade 模板 : combine string with variable from controller for @yield parameter

php - laravel 5 从未找到的数据库类中获取数据

Laravel 5.4 有时验证规则不起作用