如何使用 Laravel 的 Blade 模板将变量传递给布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16118104/
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
How do I pass a variable to the layout using Laravel' Blade templating?
提问by Dwight
In Laravel 4, my controller uses a Blade layout:
在 Laravel 4 中,我的控制器使用 Blade 布局:
class PagesController extends BaseController {
protected $layout = 'layouts.master';
}
The master layout has outputs the variable title and then displays a view:
主布局输出变量标题,然后显示一个视图:
...
<title>{{ $title }}</title>
...
@yield('content')
....
However, in my controller I only appear to be able to pass variables to the subview, not the layout. For example, an action could be:
但是,在我的控制器中,我似乎只能将变量传递给子视图,而不是布局。例如,一个动作可以是:
public function index()
{
$this->layout->content = View::make('pages/index', array('title' => 'Home page'));
}
This will only pass the $title
variable to the content section of the view. How can I provide that variable to the whole view, or at the very least the master layout?
这只会将$title
变量传递给视图的内容部分。我如何向整个视图或至少主布局提供该变量?
回答by s3v3n
If you're using @extends
in your content layout you can use this:
如果您@extends
在内容布局中使用,则可以使用:
@extends('master', ['title' => $title])
回答by Chilion
For future Google'rs that use Laravel 5, you can now also use it with includes,
对于未来使用 Laravel 5 的 Google 员工,您现在也可以将其与包含一起使用,
@include('views.otherView', ['variable' => 1])
回答by Mwirabua Tim
I was able to solve that problem by adding this to my controller method:
通过将其添加到我的控制器方法中,我能够解决该问题:
$title = 'My Title Here';
View::share('title', $title);
$this->layout->title = 'Home page'; did not work either.
$this->layout->title = '首页'; 也没有用。
回答by Rohan Krishna
In the Blade Template : define a variable like this
在 Blade 模板中:定义一个这样的变量
@extends('app',['title' => 'Your Title Goes Here'])
@section('content')
And in the app.blade.php or any other of your choice ( I'm just following default Laravel 5 setup )
在 app.blade.php 或您选择的任何其他文件中(我只是遵循默认的 Laravel 5 设置)
<title>{{ $title or 'Default title Information if not set explicitly' }}</title>
This is my first answer here. Hope it works.Good luck!
这是我在这里的第一个答案。希望它有效。祝你好运!
回答by Efra
Simplest way to solve:
最简单的解决方法:
view()->share('title', 'My Title Here');
Or using view Facade:
或者使用视图门面:
use View;
...
View::share('title', 'My Title Here');
回答by Dwight
It appears as though I can pass variables to the entire layout using attributes on the layout object, for example to solve my problem I was able to do the following:
似乎我可以使用布局对象上的属性将变量传递给整个布局,例如,为了解决我的问题,我能够执行以下操作:
$this->layout->title = 'Home page';
回答by Shiro
class PagesController extends BaseController {
protected $layout = 'layouts.master';
public function index()
{
$this->layout->title = "Home page";
$this->layout->content = View::make('pages/index');
}
}
At the Blade Template file, REMEMBER to use @ in front the variable.
在 Blade 模板文件中,请记住在变量前使用 @。
...
<title>{{ $title or '' }}</title>
...
@yield('content')
...
回答by Melvin
You can try:
你可以试试:
public function index()
{
return View::make('pages/index', array('title' => 'Home page'));
}
回答by Web Decode
just try this simple method: in controller:-
试试这个简单的方法:在控制器中:-
public function index()
{
$data = array(
'title' => 'Home',
'otherData' => 'Data Here'
);
return view('front.landing')->with($data);
}
And in you layout (app.blade.php) :
在你的布局 (app.blade.php) 中:
<title>{{ $title }} - {{ config('app.name') }} </title>
Thats all.
就这样。
回答by Anthony Vipond
$data['title'] = $this->layout->title = 'The Home Page';
$this->layout->content = View::make('home', $data);
I've done this so far because I needed in both the view and master file. It seems if you don't use $this->layout->title it won't be available in the master layout. Improvements welcome!
到目前为止我已经这样做了,因为我需要在视图和主文件中。似乎如果您不使用 $this->layout->title 它在主布局中将不可用。欢迎改进!