1
votes

I have a problem understanding how variables work inside the Laravel templating system, Blade. I set the variables in the controller, I can see them in the first view I make, but not inside the next one.

I'm using a master.blade.php file that holds the header and footer of the page, yielding the content in the middle. I can use the variable inside the master.blade.php file, but not inside the content blade file (variable undefined).

For example, for the contact page:

CONTROLLER FUNCTION:

$this->data['page'] = "contact";

$this->layout->content = View::make('pages.contact');

$this->layout->with('data', $this->data);

MASTER.BLADE.PHP:

if ($data['page'] == 'contact')

{ //do something, it works }

@yield('content')

CONTACT.BLADE.PHP:

if ($data['page'] == 'contact')

{// do something. ErrorException Undefined variable: data}

Am I missing something or is it a known restriction?

Thanks!

1

1 Answers

1
votes

The problem was that I was passing the variables only to the layout:

Instead of:

$this->layout->content = View::make('pages.contact');

$this->layout->with('data', $this->data);

I fixed it using:

$this->layout->content = View::make('pages.contact', array('data' => $this->data));

$this->layout->with('data', $this->data);

This way passing the variables to the layout, but also to that particular view.

Hope it helps someone.