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

12

117

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?

Hildahildagard answered 20/4, 2013 at 8:1 Comment(0)
R
261

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>
Reprovable answered 29/3, 2015 at 21:38 Comment(3)
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'])......Projectile
I am doing this and I am getting an undefined error on the layout page. Any ideas?Tingley
@Tingley see usage section (provide default value, like: {{ $title ?? 'My Default Title' }})Chlorous
C
51

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

@include('views.otherView', ['variable' => 1])
Camouflage answered 13/6, 2016 at 12:55 Comment(2)
this is what im looking for to finally create a component based application on laravel, thanks!Bonar
if I only pass ["one"], how do I get it in the template?Coign
I
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!

Indemnification answered 24/5, 2015 at 15:55 Comment(2)
This helped me save few hours..! Used it in Laravel 5.4 and it works great.. Thanks for the answer...Linwoodlinz
I had to do: {{ $title ?? 'Default Title' }}Optimist
E
20

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.

Excurved answered 10/6, 2013 at 7:10 Comment(1)
This worked for me in laravel 5.1. Easiest way to solve the problem.Egret
M
6

Simplest way to solve:

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

Or using view Facade:

use View;

...

View::share('title', 'My Title Here');
Minim answered 13/5, 2016 at 7:25 Comment(1)
can i execute it in a route middleware?Tarahtaran
H
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';
Hildahildagard answered 20/4, 2013 at 8:4 Comment(2)
This didn't work for me. I currently get an error stating that it does not exist as a variable.Tourbillion
where can I find the layout object?Signalman
E
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')
...
Ethnography answered 7/4, 2014 at 14:30 Comment(1)
@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.Ethnography
C
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.

Cisco answered 19/10, 2019 at 7:34 Comment(2)
Could you explain how this solves the problem? And what new does it contribute over and above the previous (especially accepted) answers?Elderberry
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.Usn
A
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"
]
*/
Autoionization answered 30/8, 2020 at 17:48 Comment(0)
G
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 
Groff answered 29/6, 2021 at 12:37 Comment(1)
It works with me on laravel 8 based project, thank you very muchGalloon
C
-1

You can try:

public function index()
{
    return View::make('pages/index', array('title' => 'Home page'));
}
Comparative answered 2/8, 2013 at 8:0 Comment(0)
P
-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!

Pifer answered 2/8, 2013 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.