如何使用 Laravel 的 Blade 模板将变量传递给布局?

在 Laravel 4中,我的控制器使用 Blade 布局:

class PagesController extends BaseController {
protected $layout = 'layouts.master';
}

主布局输出变量 title,然后显示一个视图:

...
<title>{{ $title }}</title>
...
@yield('content')
....

但是,在我的控制器中,我似乎只能将变量传递给子视图,而不能传递给布局。例如,操作可以是:

public function index()
{
$this->layout->content = View::make('pages/index', array('title' => 'Home page'));
}

这只会将 $title变量传递给视图的 content 部分。我怎样才能为整个视图或者至少是主布局提供这个变量呢?

142451 次浏览

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';

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.

$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!

You can try:

public function index()
{
return View::make('pages/index', array('title' => 'Home page'));
}
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.

...
<title>\{\{ $title or '' }}</title>
...
@yield('content')
...

If you're using @extends in your content layout you can use this:

@extends('master', ['title' => $title])

Note that same as above works with children, like:

@include('views.subView', ['my_variable' => 'my-value'])

Usage

Then where variable is passed to, use it like:

<title>\{\{ $title ?? 'Default Title' }}</title>

In the Blade Template : define a variable like this

@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 )

<title>\{\{ $title or 'Default title Information if not set explicitly' }}</title>

This is my first answer here. Hope it works.Good luck!

Simplest way to solve:

view()->share('title', 'My Title Here');

Or using view Facade:

use View;


...


View::share('title', 'My Title Here');

For future Google'rs that use Laravel 5, you can now also use it with includes,

@include('views.otherView', ['variable' => 1])

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) :

<title>\{\{ $title }} - \{\{ config('app.name') }} </title>

Thats all.

if you want to get the variables of sections you can pay like this:

$_view      = new \View;
$_sections  = $_view->getFacadeRoot()->getSections();
dd($_sections);
/*
Out:
array:1 [▼
"title" => "Painel"
]
*/

Following is simple solution worked for me.

In layout


<title>@yield('Page-Title') </title>


In your blade file


@section('Page-Title')
My Page Title
@endsection