Using Multiple Layouts in Zend Framework

Followed by my earlier post >>

Besides using default layout you can use multiple layouts or can switch between layouts. All you need to do is just pick the layout object and set the desired layout name.

Here is a scenario:

Say you have 2 layouts at application/layouts/scripts/ i.e. layout.phtml and layout1.phtml

As layout.phtml is your default layout and you need to use layout1 in some cases. To do that we need to pick the layout object as mentioned earlier, there are several ways.

Access a layout object:

At view scripts:

<?php $layout = $this->layout(); ?>

At action controller:

$layout = $this->_helper->layout();

Static method elsewhere:

$layout = Zend_Layout::getMvcInstance();

At bootstrap:

$layout = $bootstrap->getResource('Layout');

Set the desired layout (the layout will work within defined scope):

$layout->setLayout('layout1');

i.e. If you set the layout at any action controller then the layout will be viewed for that particular action.

For more reading

http://framework.zend.com/manual/en/learning.layout.usage.html

Advertisements