无法将数据从控制器传递到刀片模板 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19428212/
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
Can't pass data from controller to blade template laravel
提问by Claire
In my page controller I have
在我的页面控制器中,我有
$this->layout->content = View::make('authentication/login')->with('page_title','title');
I am using a blade file for my template. In the head of the html, I have
我正在为我的模板使用刀片文件。在 html 的头部,我有
<title>{{$page_title}}</title>
I get an error that $page_title
is undefined.
我收到一个$page_title
未定义的错误。
What I want ideally is a $data=array('page_title'=>'Login','second_item'=>'value')...
. But as I cannot get the basic passing of a variable to a view working, I am sticking with that first of all.
我理想中想要的是一个$data=array('page_title'=>'Login','second_item'=>'value')...
. 但是由于我无法将变量基本传递给视图工作,所以我首先坚持这一点。
采纳答案by Glad To Help
There are many ways to achieve this, as @Gravy pointed out, but judging by the way she is trying to write the code, the solution would be:
正如@Gravy 所指出的,有很多方法可以实现这一点,但从她尝试编写代码的方式来看,解决方案是:
$data = array();
$this->layout->with('data', $data);
$this->layout->content = View::make('home');
See more here: http://forums.laravel.io/viewtopic.php?pid=58548#p58548
在此处查看更多信息:http: //forums.laravel.io/viewtopic.php?pid=58548#p58548
回答by Gravy
$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'));
回答by foxns7
$data = array('page_title'=>'Login','second_item'=>'value');
return View::make('authentication/login', $data);
回答by David Barker
So to get layouts working in controllers you need to first declare the variable content
in the layout blade template.
所以为了让布局在控制器中工作,你需要首先content
在布局刀片模板中声明变量。
In your controller do what you have done already, but remember dot notation when working with directory structures in views. layouts.master is the same as layouts/master.blade.php.
在您的控制器中执行您已经完成的操作,但在处理视图中的目录结构时请记住点符号。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');
}
}
in layouts/master.blade.php
在布局/master.blade.php
<div class="content">
{{-- This is the content variable used for the layout --}}
{{ $content }}
</div>
in authentication/login.blade.php
在authentication/login.blade.php 中
<title>{{ $page_title }}</title>
This will work if you use this structure.
如果您使用此结构,这将起作用。