Switch layouts in Zend Framework

If you have a larger Zend Framework application, you probably want to you use a layout for more of your pages. For instance you’d need at least two layouts: one for a backend and another one for frontend. In order to tell ZF to use layouts, you’ll need to set the layout paths in your application.ini

In order to store the layouts in /application/layouts/scripts, add the following to /application/configs/application.ini

resources.layout.layoutpath = APPLICATION_PATH "/layouts/scripts"

You can now create several layouts, something around this model:




    
    headTitle() ?>
    headScript() ?>
    headStyle() ?>


    render('header.phtml') ?>

    
layout()->content ?>
render('footer.phtml') ?>

By default, your application will use the layout in /application/layouts/scripts/layout.phtml, but if you want some pages to be rendered using another layout you can tell ZF to do so like this:

// Within controller
// note that the file that's going to be rendered is /application/layouts/scripts/backend.phtml
$this->_helper->_layout->setLayout('backend')

//Within view script
$this->layout()->setLayout('other-layout'); ?>

More info on using Zend_Layout: http://framework.zend.com/manual/en/zend.layout.quickstart.html