- |-- Bootstrap.php
- |-- configs
- | `-- application.ini
- |-- controllers
- |-- layouts
- | `-- scripts
- | |-- layout.phtml
Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at docs.zendframework.com
Using Zend_Layout - Getting Started with Zend_Layout
Basic usage of Zend_Layout is fairly trivial. Assuming you're using Zend_Application already, you can simply provide some configuration options and create a layout view script.
The recommended location of layouts is in a "layouts/scripts/" subdirectory of your application:
To initialize Zend_Layout, add the following to your configuration file ("application/configs/application.ini"):
The first line indicates where to look for layout scripts; the second line gives the name of the layout to use, minus the view script extension (which is assumed to be ".phtml" by default).
Now that you have your configuration in place, you need to create your layout script. First, make sure that you've created the "application/layouts/scripts" directory; then, open an editor, and create the markup for your layout. Layout scripts are simply view scripts, with some slight differences.
In the example above, you'll note the call to a layout() view helper. When you register the Zend_Layout resource, you also gain access to both an action and view helper that allow you access to the Zend_Layout instance; you can then call operations on the layout object. In this case, we're retrieving a named variable, $content, and echoing it. By default, the $content variable is populated for you from the application view script rendered. Otherwise, anything you'd normally do in a view script is perfectly valid -- call any helpers or view methods you desire.
At this point, you have a working layout script, and your application is informed of its location and knows to render it.
On occasion, you may need direct access to the layout object. There are three ways you can do this:
Within view scripts: use the layout() view helper, which returns the Zend_Layout instance registered with the front controller plugin.
Since it returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.
Within action controllers: use the layout() action helper, which acts just like the view helper.
As with the view helper, since the action helper returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.
Elsewhere: use the static method getMvcInstance(). This will return the layout instance registered by the bootstrap resource.
Via the bootstrap: retrieve the layout resource, which will be the Zend_Layout instance.
Anywhere you have access to the bootstrap object, this method is preferred over using the static getMvcInstance() method.
In most cases, the above configuration and layout script (with modifications) will get you what you need. However, some other functionality exists you will likely use sooner or later. In all of the following examples, you may use one of the methods listed above for retrieving the layout object.
Setting layout variables. Zend_Layout keeps its own registry of layout-specific view variables that you can access; the $content key noted in the initial layout script sample is one such example. You can assign and retrieve these using normal property access, or via the assign() method.
disableLayout(). Occasionally, you may want to disable layouts; for example, when answering an Ajax request, or providing a RESTful representation of a resource. In these cases, you can call the disableLayout() method on your layout object.
The opposite of this method is, of course, enableLayout(), which can be called at any time to re-enable layouts for the requested action.
Selecting an alternate layout: If you have multiple layouts for your site or application, you can select the layout to use at any time by simply calling the setLayout() method. Call it by specifying the name of the layout script without the file suffix.
The layout script should reside in the $layoutPath directory specified in your configuration. Zend_Layout will then use this new layout when rendering.