Search code examples
phplithium

PHP - Lithium framework How to change/switch default layout


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.


Solution

  • 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);
    });