无法将数据从控制器传递到刀片模板层

时间:2013-10-17 13:33:54

标签: php laravel laravel-4

在我的页面控制器中,我有

$this->layout->content = View::make('authentication/login')->with('page_title','title');

我正在为我的模板使用刀片文件。在html的头部,我有

<title>{{$page_title}}</title>

我收到$page_title未定义的错误。

我理想的是$data=array('page_title'=>'Login','second_item'=>'value')...。但由于我无法将变量基本传递给视图工作,所以我首先要坚持这一点。

4 个答案:

答案 0 :(得分:3)

有许多方法可以实现这一点,正如@Gravy指出的那样,但从她尝试编写代码的方式来判断,解决方案将是:

$data = array();
$this->layout->with('data', $data);
$this->layout->content = View::make('home');

在此处查看更多内容:http://forums.laravel.io/viewtopic.php?pid=58548#p58548

答案 1 :(得分:2)

$data = 
[
    'page_title' => 'Login',
    'second_item' => 'value'
    ...
];

return View::make('authentication/login', $data);

// or

return View::make('authentication/login', compact('data'));

// or

return View::make('authentication/login')->with($data);

// or

return View::make('authentication/login')->with(['page_title' => 'Login', 'second_item' => 'value']);

// or

return View::make('authentication/login')->with(array('page_title' => 'Login', 'second_item' => 'value'));

答案 2 :(得分:1)

$data = array('page_title'=>'Login','second_item'=>'value');
return View::make('authentication/login', $data);

答案 3 :(得分:0)

因此,要使布局在控制器中工作,您需要首先在布局刀片模板中声明变量content

在您的控制器中执行您已经完成的操作,但在视图中使用目录结构时请记住点符号。 layouts.master与layouts / master.blade.php相同。

class UserController extends BaseController {
    /**
     * The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    public function getIndex()
    {
        // Remember dot notation when building views
        $this->layout->content = View::make('authentication.login')
                                     ->with('page_title','title');
    }
}
layouts / master.blade.php

中的

<div class="content">
    {{-- This is the content variable used for the layout --}}
    {{ $content }}
</div>
authentication / login.blade.php

中的

<title>{{ $page_title }}</title>

如果您使用此结构,这将有效。

相关问题