Laravel 模板 - 将数据传递给布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14826506/
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 template - passing data to layout
提问by Ram Guiao
How to pass data to layout file? I can only access the passed data on the content page but not on the layout file.
如何将数据传递给布局文件?我只能访问内容页面上传递的数据,而不能访问布局文件上的数据。
public function get_index($name){
return View::make('widget.'.$name)
->with("title", ucwords($name).' ‹ Document Management System');
}
回答by Muhammad Usman
Use
用
View::share('data', $data);
on your before
filter or in __construct
of your Base_Controller
.
在您的before
过滤器上或在__construct
您的Base_Controller
.
回答by Dave Ganley
You need a global view variable. I think you need to look at View::share('title', $title);
I also think you can chain it with ->shares('title', $title)
您需要一个全局视图变量。我认为你需要看看View::share('title', $title);
我也认为你可以将它与->shares('title', $title)
回答by Steven Mercatante
You can also use $this->layout->with('foo', 'bar')
from within an action to make a variable foo
available to your layout.
您还可以$this->layout->with('foo', 'bar')
在操作中使用from 使变量foo
可用于您的布局。
回答by Niall O'Brien
I had a situation recently where I needed to dynamically change the @include('layouts._sidebarLeft') statement in my master template. View::share($key, $value) solves this. I then changed my template to @include($key) and whenever I need to change it from the default one, I just run another View::share() before returning the view in my controller. I defined the default sidebar within /start/global.php
我最近遇到了一种情况,我需要动态更改主模板中的 @include('layouts._sidebarLeft') 语句。View::share($key, $value) 解决了这个问题。然后我将我的模板更改为 @include($key) 并且每当我需要将它从默认的更改时,我只需在我的控制器中返回视图之前运行另一个 View::share() 。我在 /start/global.php 中定义了默认的侧边栏