Zend Framework: working with layout and views (two step view)

19 Mar

In this article I’m going to discuss layout and views and will give a practical example.

You may have heard of two step view.

The term two step view is used when we use layout and view templates for making our page look and feel.

Mostly you will need header, menu and footer same for the entire web application, only contents in the center of the page changes. So if this is the case you can get benefits out of two step views.

Create a layout containing header, menus and footer etc and view will have the contents relevant that specific page.

To implement two step view  in Zend framework you will need to make some configuration in bootstrap file.

Add the following lines to your bootstrap file.

$options = array(

    'layout'     => 'layout_name',

    'layoutPath' => '/path/to/layouts',


$layout = Zend_Layout::startMvc($options);

Keep in mind two things.
1.    Specify correct layout_name.
2.    Path to the layout file must be correct.
I usually prefer to create my layout directory in scripts directory under application/views.
If you create layout.phtml in your layouts directory you will need to define it as follows.

        include "header.phtml";
// view contents goes here.
// footer goes here.
        include "footer.phtml"

Now whatever request (controller/action) you made to your page, header and footer will be included in the response.

There may be cases (in some of your actions) where you don’t want your header and footer be appeared in the look and feel.

So write in those actions, the following line of code


After including this line in your action, only the view contents will appear. It mean the contents in the header and footer will not be served.

Some time you need to execute only action code and redirect your request to another action instead of showing anything.

Write the following code in this case



Another very important thing is when you call a specific action, Zend Framework render a template file with the name similar to the action name. for example if you request view action, Zend will look for view.phtml file in the specific template directory.

If you want to render other then the default template, view.phtml in the above example. Write the following in your action.



7 Responses to “Zend Framework: working with layout and views (two step view)”

  1. magentocoder April 25, 2009 at 2:09 pm #

    nice and helpful article

  2. darkvador May 14, 2009 at 1:16 pm #


    am wondering if you could help in using jqgrid with.

    I was able to display the layout of the grid but since am quite new to zend framwork am working if you could help in retrieve and displaying the data in the grid .

    In my controller , in the index action , how do i passed a select query so as it get displayed in the jqgrid.

    Thank you for precious help

  3. Abhishek May 19, 2009 at 2:30 am #

    Can you help me in this?

    I want to call 1 .phtml file inside a class to render it?
    Basically, I am trying to embed another part of my application? How can i do it?

  4. Faheem Abbas May 19, 2009 at 5:25 am #

    To render another template file simply write
    Don’t write an extension as it default apply .phtml

  5. Marco October 28, 2009 at 10:22 pm #

    could explain this bit: layout()->content?>
    it gives the following error in php… “Parse error: syntax error, unexpected ‘<' in "

  6. Jay March 30, 2010 at 8:24 pm #

    Re: Marco


    should be

    echo $this->layout()->content;

    since it’s already in <?php tags the short tag there shouldn't be used.

  7. maggi November 1, 2010 at 7:43 pm #

    I need to include seperate header and footer for each controller. I mean I have 3 controllers and I need to include different header and footer. Is it possible????

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: