将Laravel路径中的数组传递给Blade模板

时间:2014-05-28 05:28:08

标签: arrays laravel blade

我正在尝试将一系列值从Laravel中的routes.php传递到Blade模板中的@foreach循环。这是我的路线例程:

Route::get('/', function()
{
    $theColors = array("red","green","blue","yellow");
    return View::make('hello', array('theLocation' =>  'NYC', 'theWeather'=> 'stormy', 'theColors'));
});

我的Blade模板代码:

    @foreach ($theColors as $color)
    <p>The color is {{$color}}.</p>
    @endforeach

我的日志显示模板中的变量 - $theColors - 未定义。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您尚未正确地将$theColors传递给视图

更改

return View::make('hello', array('theLocation' =>  'NYC', 'theWeather'=> 'stormy', 'theColors'));

return View::make('hello', array('theLocation' =>  'NYC', 'theWeather'=> 'stormy', 'theColors' => $theColors));