How do I pass a variable to the layout using Laravel' Blade templating?

Dwight picture Dwight · Apr 20, 2013 · Viewed 109.2k times · Source

In Laravel 4, my controller uses a Blade layout:

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?

Answer

s3v3n picture s3v3n · Mar 29, 2015

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

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