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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:33:22  来源:igfitidea点击:

Laravel template - passing data to layout

phplaravel

提问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 beforefilter or in __constructof 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 fooavailable 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 中定义了默认的侧边栏