Reputation: 25277
Usually I have managed my layouts in Laravel like this:
views/index.blade.php
<html>
<body>
@yield('content')
</body>
</html>
views/main/root.blade.php
@extends('index')
@section('content')
<p>whatever</p>
@stop
controllers/MainController.php
class MainController extends \BaseController {
public function root(){
return View::Make('main.root');
}
}
Now I am reading about the $layout variable. The documentation says:
Your application probably uses a common layout across most of its pages. Manually creating this layout within every controller action can be a pain. Specifying a controller layout will make your development much more enjoyable
But I do not see how this makes it more enjoyable.
This is the same code, but using the $layout variable:
controllers/MainController.php
class MainController extends \BaseController {
public $layout = "index";
public function root(){
$this->layout->nest('content', 'main.root');
}
}
Now, how is this easier? It seems like more code to me. Besides I have already stated that root
blade extends index
so it seems like there is duplication here.
I probably am getting something wrong about this technique. Can someone help me to make sense of it to me?
Upvotes: 0
Views: 3188
Reputation: 25277
I have been asking on IRC and it seems like this is actually coming from Laravel 3, and now @extends is the new way of doing things. So it appears setupLayout
is sort of legacy code. So I guess I can safely ignore it.
Upvotes: 0
Reputation: 8029
The point is
@extends
in every single viewWhether or not it is actually good practice or if it saves you any pain is definitely arguable, but that's the idea behind it.
Upvotes: 1
Reputation: 3026
If you set up your BaseController: (Laravel calls setupLayout()
automatically if it exists)
class BaseController extends Controller {
protected $layout = 'layouts.master';
protected function setupLayout()
{
$this->layout = View::make($this->layout);
}
}
You can just specify the @section()
name as a property, not have to @extend()
your views. and / or override the layout inherited form BaseController.
class MainController extends \BaseController {
public function index(){
$this->layout->content = View::make('main.index');
}
}
In your view:
@section('content')
<div class="row-fluid">
Test
</div>
@stop
In the master layout:
@yield('content')
Upvotes: 1