6

I googled two hours, but not found answer. Maybe you can help.

When I define in MyController:

class MyController extends Base_Controller {
    public $layout = 'layouts.default';

    public function get_index() {
        $entries = Entry::all();
        return View::make('entries.index')
            ->with('entries', $entries);
        }
    }
}

In entries\index.blade.php:

@section('content')
    <h1>Test</h1>
@endsection

And in layouts\default.blade.php:

<!DOCTYPE html>
<html>
<body>
    @yield('content')
</body>
</html>

Nothing is displaying. And I don't understand why. When I am replacing in MyController return part with:

$this->layout->nest('content', 'entries.index', array(
    'entries' => $entries
));

Then all is working, but.. It looks not clean and I don't like it. When adding in every view @layout('layouts.default') all is working good too, but it is not DRY. For example, in RoR I don't need to do such things in Controller.

How can define in MyController one layout and use return View::make (I think that this is right way) or how can do it better?

Charles
  • 50,943
  • 13
  • 104
  • 142
Orbitum
  • 1,585
  • 5
  • 27
  • 47

3 Answers3

15

To use layouts in controllers, you must specify:

public $layout = 'layouts.default';

You can also not return in the method as it will override the use of $layout. Instead, to embed your content within the layout you use:

$this->layout->nest('content', 'entries.index', array('entries' => $entries));

No need to return anything in your method now. This will fix it.


Edit:

"Beautiful Ways?"

$this->layout->nest('content', 'entries.index')->with('entries', $entries);


$this->layout->content = View::make('entries.index')->with('entries', $entries);


$this->layout->entries = $entries;
$this->layout->nest('content', 'entries.index');
Joel Larson
  • 3,064
  • 1
  • 15
  • 12
  • Yes - it is working as I wrote in question post, but IMHO it is ugly. There are no clearer/simpler way? – Orbitum Nov 21 '12 at 21:01
  • @Orbitum what exactly is ugly? – afarazit Nov 21 '12 at 21:05
  • It is strange to do `$this->layout->nest('section', 'viewfile')` for each action. So I want to know - is there more laconic way to render view with/without section and variables or no. If there are no shorter way - then I will not search more. – Orbitum Nov 21 '12 at 21:09
  • 2
    Remember you can chain the with() and nest() methods. `$this->layout->with('title', 'Hello World')->nest('content', 'entries.index', compact('entries'))` – Jason Lewis Nov 22 '12 at 06:31
  • $this->layout->nest('content', 'entries.index')->with('entries', $entries); not working.. entries is undefined – Jaime Sangcap Mar 24 '14 at 08:43
0

It should be

public $layout = 'layouts.default';

Here's the link Templating - The Basics

Now you can return your layout like this

$view = View::make('entries.index')->with('entries', $entries);
$this->layout->content = $view->render();
afarazit
  • 4,907
  • 2
  • 27
  • 51
-1
 class BaseController extends Controller {

/**
 * Setup the layout used by the controller.
 *
 * @return void
 */

/*Set a layout properties here, so you can globally
  call it in all of your Controllers*/
protected $layout = 'layouts.default';

protected function setupLayout()
{
    if ( ! is_null($this->layout))
    {
        $this->layout = View::make($this->layout);
    }
}

}

class HomeController extends BaseController {

public function showHome()
{   
    /*now you can control your Layout it here */
     $this->layout->title= "Hi I am a title"; //add a dynamic title 
     $this->layout->content = View::make('home');
}

}

Ref: http://teknosains.com/i/tutorial-dynamic-layout-in-laravel-4

jsdev
  • 672
  • 6
  • 14
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Praveen Prasannan Jan 29 '14 at 17:55