Zend Framework Layouts Overview
Thursday, October 8, 2009
The Zend Framework is shipped with a template module called Zend_Layout. This allows the specific content for a page to be injected into one or more places in some standard content.
The standard content is contained in a layout script, which is much like a view script. The script will contain the following piece of PHP code, or something very like it, to inject the content rendering by the view script into the layout script:
echo $this->layout()->content;
There are several ways to turn on the template engine; one has been covered in the post Bootstrapping the Application: the Standard Layout Resource Plugin. Note that the location of the layout scripts is specified in the configuration file settings. It is also possible to set the location directly from the controller, like this:
$this->_helper->layout->setLayoutPath(APPLICATION_PATH . "/layouts/scripts");
Note that the layout is accessed from the controller via the helper broker $this->_helper; this object is configured to expose all the available helpers as if they were member variables of that object.
By default, the engine looks for a script called layout.phtml; if you want to use a different script, you need to specify it explicitly. For instance, the application also uses the engine to render the contact e-mail; the name of the script it uses is contact-email.phtml. The following code would instruct the engine to use this script:
$this->_helper->layout->setLayout('contact-email');
If you want to disable the use of the layout for a particular action, include the following call in its handler:
$this->_helper->layout->disableLayout();
In the next few posts, we shall look at the main layout script for the sample application in more detail.

Practical Web 2.0 Applications with PHP (Expert's Voice) by Quentin Zervaas
Beginning Databases with PostreSQL: From Expert to Professional 2nd Edition: From Novice to Professional by Neil Matthew, Richard Stones