Home » Php » Laravel template – passing data to layout

Laravel template – passing data to layout

Posted by: admin November 30, 2017 Leave a comment

Questions:

How to pass data to layout file? I can only access the passed data on the content page but not on the layout file.

public function get_index($name){
    return View::make('widget.'.$name)
            ->with("title", ucwords($name).' ‹ Document Management System');
}
Answers:

Use

View::share('data', $data);

on your before filter or in __construct of your Base_Controller.

Questions:
Answers:

You need a global view variable. I think you need to look at View::share('title', $title);
I also think you can chain it with ->shares('title', $title)

Questions:
Answers:

You can also use $this->layout->with('foo', 'bar') from within an action to make a variable foo available to your layout.

Questions:
Answers:

I had a situation recently where I needed to dynamically change the @include(‘layouts._sidebarLeft’) statement in my master template. View::share($key, $value) solves this. I then changed my template to @include($key) and whenever I need to change it from the default one, I just run another View::share() before returning the view in my controller. I defined the default sidebar within /start/global.php