I have a layout with 4 separate “chunks”. They are:
- A nav panel with a menu and breadcrumbs. This is constructed using Zend_Navigation.
- A sidebar, which shows general “news” by default
- A content area, where the main output from each controller action would be placed
- A header area, which is the above the navigation, which normally displays just some stock text and a photo.
The content area fits in with the traditional single view model that the documentation for Zend_Application states, but the other three do not. They all have reasonable default views to use, but a controller needs to be able to override them if needed. For example, it makes sense for an administration page to override the “newsy” view to display a log of recent administrative actions taken on the system.
The Zend_Layout/Zend_Application examples, however, all assume a single view (they call <?php echo $this->layout()->content; ?>.
How can one accomplish this kind of overriding of the layout? The only solution I’ve thought of would be to store overridden Zend_Views inside Zend_Registry, but that seems like holding things together with duct tape 😉
I believe what you are referring to are called “Named segments”. Zend Framework’s response object has support for these so called “named segments”, and will allows you to segregate body content into different segments.
For example take the following layout file:
Here you have 2 named segments, being “content” and “nav”. By default, output from your view scripts will be rendered to the “content” segment. In order to render output to the “nav” segment, you could do the following in a controller:
This feature gets particularly useful when used in conjunction with the ActionStack action helper. Take for instance that you are at an administration page, and you would like to override the “nav” section, then you could determine per controller which part you want render to that segment. The best way to explain this would be by a code example: