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

5 thoughts on “Using Multiple Layouts in Zend Framework

  1. keep a new layout as newlayout.phtml in the layouts folder, and then from the action load it like

    $this->_helper->layout->setLayout(‘newlayout’);
    🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s