113

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?

JackPoint
  • 4,031
  • 1
  • 30
  • 42
Dwight
  • 12,120
  • 6
  • 51
  • 64

12 Answers12

256

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>
s3v3n
  • 8,203
  • 5
  • 42
  • 56
  • 2
    It took me a lot of googling to find this! It is just this syntax that you got to get perfect and docs don't show all these subtleties. I mean this is what official docs say `@component('alert', ['foo' => 'bar'])`...... – Arthur Tarasov Jan 18 '18 at 08:28
  • I am doing this and I am getting an undefined error on the layout page. Any ideas? – LucyTurtle Mar 15 '18 at 00:12
  • @LucyTurtle see usage section (provide default value, like: `{{ $title ?? 'My Default Title' }}`) – Top-Master Jan 05 '22 at 15:12
51

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

@include('views.otherView', ['variable' => 1])
Chilion
  • 4,380
  • 4
  • 33
  • 48
23

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!

Rohan Krishna
  • 417
  • 4
  • 10
19

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.

Timothy
  • 4,198
  • 6
  • 49
  • 59
5

Simplest way to solve:

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

Or using view Facade:

use View;

...

View::share('title', 'My Title Here');
Efra
  • 121
  • 2
  • 4
4

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';
JackPoint
  • 4,031
  • 1
  • 30
  • 42
Dwight
  • 12,120
  • 6
  • 51
  • 64
1
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')
...
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
Shiro
  • 7,344
  • 8
  • 46
  • 80
  • @YevgeniyAfanasyev, this question you should ask back post owner. I just refer back his coding. Kind of weird you ask back me.... And this was answered in 2014. Of course I was referring on the version he asking which is Laravel 4. – Shiro Oct 25 '18 at 00:44
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.

Web Decode
  • 11
  • 1
  • Could you explain how this solves the problem? And what new does it contribute over and above the previous (especially accepted) answers? – gstukelj Oct 19 '19 at 07:46
  • Welcome to SO! When you place an answer, even if it is ok, you should explain it a little bit, and in your case, as there are one similar, try to explain the pros and cons of your answer. – David García Bodego Oct 19 '19 at 07:58
1

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"
]
*/
DEV Tiago França
  • 1,271
  • 9
  • 9
1

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 
Kunal Rajput
  • 664
  • 1
  • 7
  • 21
-1

You can try:

public function index()
{
    return View::make('pages/index', array('title' => 'Home page'));
}
Melvin
  • 5,798
  • 8
  • 46
  • 55
-2
$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!

Anthony Vipond
  • 1,879
  • 2
  • 19
  • 21