concept Zend_Layout in category php

appears as: Zend_Layout
Zend Framework in Action

This is an excerpt from Manning's book Zend Framework in Action.

The view script system is called Zend_View, which provides a PHP-based template system. This means that, unlike Smarty or PHPTAL, all the view scripts are written in PHP. Zend_View provides a helper plug-in system to allow for the creation of reusable display code. It is designed to allow for overriding for specific requirements, or even for using another template system entirely, such as Smarty. Working in conjunction with Zend_View is Zend_Layout, which provides for aggregating multiple view scripts to build the entire web page.

We have already looked at the basics of Zend_View in Chapters 2 and 3. In this chapter, we will look in more detail at the more advanced view helpers supplied with the framework. We’ll also look at the Zend_Layout component, which provides for a Two Step View integrated with a Composite View system allowing for very flexible page displays. First, though, let’s look at what the Composite View design pattern is and why we would want to use it.

To integrate Zend_Layout into the Places website, we need to start with the bootstrap class. Listing 4.1 shows the change we need to make to the runApp() function.

Listing 4.1. Starting Zend_Layout in application/bootstrap.php

As explained in section 4.1, we use the startMvc() method to set up the Zend_Layout object and also register the associated front controller plug-in and action helper. The default settings for Zend_Layout are acceptable for the Places site, except that we need to specify the directory for the layout scripts . We have chosen views/layouts in the application directory, as this separates the layout files from our script files.

The Zend_Layout component manages rendering of a master layout script, which contains content placeholders for embedding content generated by actions or other view scripts. As with all Zend Framework components, Zend_Layout works with the minimum of configuration for most uses, but if your requirements are more specialized, it is very flexible.

When using Zend_Layout with the MVC components, the startMvc() method is used to initialize it. This is done in the bootstrap file like this:

Zend_Layout::startMvc(array('layoutPath' => '/path/to/layouts'));

As you can see, this is a static method, so you don’t need to create an instance of the Zend_Layout class first. Behind the scenes, startMvc() creates a Singleton instance of Zend_Layout and registers a front controller plug-in and an action helper that can then be interfaced with from the rest of the application. The front controller plug-in, Zend_Layout_Controller_Plugin_Layout, has a postDispatch() hook function that renders the layout template at the end of the last dispatched action. The action helper, Zend_Layout_Controller_Action_Helper_Layout, is used to provide easy access to the Zend_Layout object from within a controller.

The startMvc() function takes an array of options, but only layoutPath is required. This specifies the directory where the layout files are stored. By default, the layout.phtml file is rendered by Zend_Layout. However, you can easily change this in any controller method, such as init(), with this statement: