In the first part, we looked at the simple way to do 2 step views in Zend Framework prior to 1.5 using a front controller plugin. Now, with the release of 1.5, we have access to Zend_Layout, which is native support for implementing 2 step views.
First off, Visit Akra’s site, there is a lot of information of the Zend Framework there, and a great tutorial on Zend_Layout. You can find the article here, http://akrabat.com/2007/12/11/simple-zend_layout-example/, Also take a look at the official documentation here, http://framework.zend.com/manual/en/zend.layout.html
First off, I’ll assume you have a functioning bootstrap, and working application.
Inside your bootstrap you must start Zend_Layout’s MVC Helper. This can take a number of arguments in array fashion, the most important being the directory that you are going to store the templates in. I suggest views/layouts. See the official documentation for more options.
Zend_Layout::startMvc(array('layoutPath' => '../application/views/layouts'));
Now, before we go further, Here are some methods you can use in the controllers in your application.
// These methods to be used inside controllers
//Disable using layout, great for just output, debugging, and AJAX requests
$this->_helper->layout->disableLayout();
// Change layout script to use
$this->_helper->layout->setLayout('template2');
Now, by default, Zend_Layout uses ‘layout.phtml’ as the default layout script. This should be located in the folder you specified in the bootstrap.
You can access an instance of Zend_Layout in the view by using $this->layout(); , so wherever you want to render the output in your script you should echo $this->layout()->content; , make sense? Let’s look at an example ‘layout.phtml’;
<html> <head><title><?php echo $this->title; ?></title></head> <body> <div id="content"><?php echo $this->content; ?></div> </body> </html>
Now this is just a demo, no a valid document, but you get the idea. I also wanted to point out, that while using Zend_Layout, you can access your helpers and view variables in the same manner as before.
But wait, there is something else great about this new component. It will take all segments from the response object and assign them a view variable, this way you can dynamically add widgets such as navigation to your pages. Here is the example from Akra.
//Located in controller
$response = $this->getResponse();
$response->insert('sidebar', $this->view->render('sidebar.phtml'));
//Now inside your layout.phtml
<?php echo $this->layout()->sidebar; ?>
Very, Very Simple! Also , from what I’m learning, the ActionStack helper can be used to even further expand the use of the response object in conjunction with Zend_Layout, see the official documentation for Zend_Layout for more info, I may make a write up later when I’m more educated on this.
So you see this native support that Zend has added is very simple to use, and like every aspect of the framework, is very easy to extend, and when used with other components and modules, is very powerful.
Thank you contributors, and Zend for this addition.


August 23rd, 2008 at 4:35 pm
Hello Adam,
First, thanks for this Zend layout example. I m new to zend. Its very difficult for me to understand very thing. I have only built a app http://www.mbstu.ac.bd/ using procedural PHP. I m using OOP features for about a month or so. I m interested in using zend for my future projects.
Do you think this being early to use zendF in my development carrier.
And it seems to me sites using ZF takes longer to load.In Bangladesh we have poor internet connectivity. So that’s a problem for us.
And lastly, whats the difference between siteTamplate using dispatchLoopStartup() preDispatch()…..
and
Layout>siteTamplate
And which one to use?
August 28th, 2008 at 2:45 pm
I have been stuck at a job hellhole that uses PHP4 only
so havent done much more reading up on ZF.
Speed issues are normal when getting started, I forget what usually causes it, but it is fixed pretty easily, a newer release will probably fix it out of the box.
As for being too early, you might want some more exposure to OOP. The power with ZF comes from extending and using only what you need, so it relies heavily on OOP methodology, however experimenting and learning ZF while you learn OOP is probably the best route, because learning any framework has a steep curve, and all the practice you get is well spent.