2

In PHP Lithium, how to change default layout? I have two layouts for different views, and I want to switch them in different views, how can I make this happen?

Thanks.

Tom
  • 4,612
  • 4
  • 32
  • 43

1 Answers1

6

You switch the layout in the controller. So when you are done in the controller you call render this way.

return $this->render(array('layout' => 'someFancyLayout'));

You should also be able to do this in your controller. I'm not sure but you might need to extend the Controller to use this way.

 $this->_render['layout'] = 'someFancyLayout';

Please note that you can also set up custom media handlers. This is for instance used when requestting GPX-files and in that case I do not which to use a layout.

Media::type('gpx', 'application/text', array(
    'view' => 'lithium\template\View',
    'layout' => false,
    'template' => false
));

And you can also do it with a filter on the renderer call:

Media::applyFilter('render', function ($self, $params, $chain) {
    $params['options']['layout'] = 'default';
    if (someCondition == isMet) {
        $params['options']['layout'] = 'anotherLayout';
    }
    return $chain->next($self, $params, $chain);
});
Nils
  • 2,041
  • 1
  • 15
  • 20
  • Too late to ask, but I want to know how would I do this? I made a layout named `page.html.php` and put it in the `layouts` directory. I'm calling it the way you specified from a method named `page` in my Controller. But I get an error saying the template is not found in the `views\controllerName` directory. Am I doing it right? Shouldn't all the layouts be in the `layouts` folder? – Rutwick Gangurde Jun 25 '15 at 09:44