Laravel Blade:将数组作为参数传递给 yield 部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35459052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Blade: pass array as parameter to yield section
提问by Pascal Cloverfield
i would like to pass an array as parameter from my controller to the blade template.
我想将一个数组作为参数从我的控制器传递到刀片模板。
My controller looks like this:
我的控制器看起来像这样:
$myArray = array('data' => 'data');
return View::make('myTableIndex')
->nest('myTable', 'my_table_template', $myArray)
In my blade template i've got a yield like this:
在我的刀片模板中,我有这样的收益:
@yield('myTable', $myArray)
But i've get the error:
但我得到了错误:
Error: Array to string conversion
That's because the yield function only accepts strings, right?
那是因为 yield 函数只接受字符串,对吧?
Background is: I want a table template which i can use dynamically for multiple purpose or multiple data, so i can use the same template for multiple tables and just pass the columns and content as array.
背景是:我想要一个可以动态用于多种用途或多种数据的表格模板,所以我可以对多个表格使用相同的模板,只需将列和内容作为数组传递。
How can i pass an array to my yield section?
如何将数组传递给我的收益部分?
回答by The Alpha
You may use a separate file and include the file using @include
while you may pass the data with a dynamic variable name so you'll be able to use that variable name in your included view, for example:
您可以使用单独的文件并包含文件,@include
而您可以使用动态变量名称传递数据,以便您能够在包含的视图中使用该变量名称,例如:
@include('view.name', ['variableName' => $array])
So, in the view.name
view you can use/access the $array
using $variableName
variable and you are free to use any name for variableName
.
因此,在view.name
视图中您可以使用/访问$array
using$variableName
变量,并且您可以自由地为variableName
.
So, in the separate view i.e: view.name
, you may use a section
and do whatever you want to do with $variableName
.
因此,在单独的视图 ie: 中view.name
,您可以使用 asection
并做任何您想做的事情$variableName
。
Note: The problem was solved in the comment section but added as an answer here for the future reference so any viewer come here for similar problem will get the answer easily.
注意:问题已在评论部分解决,但在此处添加为答案以供将来参考,因此任何来这里解决类似问题的观众都可以轻松获得答案。