¿Cómo paso una variable al diseño usando la plantilla Blade de Laravel?

91

En Laravel 4, mi controlador usa un diseño Blade:

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

El diseño maestro genera el título de la variable y luego muestra una vista:

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

Sin embargo, en mi controlador, parece que solo puedo pasar variables a la subvista, no al diseño. Por ejemplo, una acción podría ser:

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?

Dwight
fuente

Respuestas:

217

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

@extends('master', ['title' => $title])
s3v3n
fuente
1
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
I am doing this and I am getting an undefined error on the layout page. Any ideas?
LucyTurtle
45

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

@include('views.otherView', ['variable' => 1])
Chilion
fuente
this is what im looking for to finally create a component based application on laravel, thanks!
Ron Michael
if I only pass ["one"], how do I get it in the template?
Javier J Solis Flores
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.

Tim Truston
fuente
This worked for me in laravel 5.1. Easiest way to solve the problem.
CodeChuck
16

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
fuente
This helped me save few hours..! Used it in Laravel 5.4 and it works great.. Thanks for the answer...
Saiyan Prince
2
I had to do: {{ $title ?? 'Default Title' }}
Damien Ó Ceallaigh
5

Simplest way to solve:

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

Or using view Facade:

use View;

...

View::share('title', 'My Title Here');
Efra
fuente
can i execute it in a route middleware?
mukade
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';
Dwight
fuente
8
This didn't work for me. I currently get an error stating that it does not exist as a variable.
Adam Libonatti-Roche
where can I find the layout object?
Yevgeniy Afanasyev
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')
...
Shiro
fuente
@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
0

You can try:

public function index()
{
    return View::make('pages/index', array('title' => 'Home page'));
}
Melvin
fuente
0

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
fuente
Could you explain how this solves the problem? And what new does it contribute over and above the previous (especially accepted) answers?
gst
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
0

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
fuente
-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
fuente