Reputation: 2133
I wanted to ask how can I define a multiple layouts for the same controller in Laravel. The scenario here is like the following:
I have a controller Home and i have two actions in this controller one called steps and the other called login.
I want the both of them load different layout.
The way that I used to make this is as follow:
protected $layout = "layouts.page";
public function index()
{
// Get to the page of the website making steps
$this->layout->content = View::make('steps');
}
Can I define multiple layouts? Maybe passing an array as follow:
protected $layout = array('first' => "layouts.page", 'second' => 'layouts.second');
Upvotes: 0
Views: 10511
Reputation: 3695
I achieve in this way
$this->layout = View::make('layout.master');
$this->layout->content = View::make('step.demo')
Upvotes: 3
Reputation: 25122
The way i do this is quite similar to @fideloper's answer.
protected $layout;
private $_layout = null;
public function __construct()
{
}
private function _setupLayout()
{
if ( ! is_null($this->_layout))
{
$this->layout = View::make($this->_layout);
}
}
public function home() {
$this->_layout = 'layouts.1col_public';
$this->_setUpLayout();
$this->layout->content = View::make('static/home');
}
public function about() {
$this->_layout = 'layouts.2col_public';
$this->_setUpLayout();
$this->layout->active_menu = 'about';
$this->layout->content = View::make('static/default');
}
Upvotes: 1
Reputation: 12293
If you look at the BaseController, which your controller likely extends, you'll see the layout variable is ultimately used simply as th e result of any old View.
In other words, your $layout
variable is just a View. You can create any $layout
variable in your controller:
<?php
class MyController extends BaseController {
protected $layout;
protected $layout_alt;
// Here we're over-riding setupLayout() from
// the BaseController
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
if ( ! is_null($this->layout_alt))
{
$this->layout_alt = View::make($this->layout_alt);
}
}
}
Then in your view, you can return:
$this->layout_alt->content = View::make('steps');
Of course, the possibilities are endless as Abishek R Srikaanth pointed out. You can do fancy things with Blade as well :D
Upvotes: 1
Reputation: 9007
This isn't common practise, and I haven't tested it yet, but it's worth a try.
In your controller's method:
$this->layout = View::make('layouts.master1");
Upvotes: 0
Reputation: 11691
Use View Composers or look at the section passing sub-views to views under http://laravel.com/docs/responses#views.
You can also specify multiple sections for the layout that is defined at http://laravel.com/docs/templates#blade-templating
EDIT:
If you want to define a master layout for different views from the same controller, then define the layout on the View it self. Take a look at the section Using A Blade Layout
The @extends
is used to define the layout on the view itself.
Hope this helps for what you are looking for.
Upvotes: 2
Reputation: 4921
Best solution is to create a method to generate your view, nesting your multiples layouts :
return View::make('layouts.master', array())
->nest('section_one', YOUR_SECOND_MASTER, array())
->nest...
and stop setting protected $layout
with a layout.
Upvotes: 3