This was an article I began writing several months ago. I didn’t want to abandon it, so I’m finishing it up now. It’s merely an informational and doesn’t contain a lot of depth. It’s meant for the introductory users.
For those PHP developers who have been frustrated with how to create a standard layout for pages other than having to remember to put an opening div block after the header includes and a closing div block before the footer includes, listen up. Things are about to get much easier. In fact, those PHP developers who have done ASP.Net programming in the past, you’re about to get much more pleased with PHP. The only drawback, which is more of a gain anyways, is you have to use the Zend Framework. Note that this topic is for those people who are not blessed withing already using a layout or template engine and are forced to do things the old way. Those of you who are using something like Smarty may not benefit from this, but keep reading anyways. :-D
Meet Zend_Layout. This little friendly class is going to rock your world. Imagine, for a second, that you can create a single file with all of the static content pre-layed and in an almost strictly-HTML format and stop having to use include() or require() statements to make sure headers, footers, and other static content are loaded into your files. Imagine further that you can use this same file to load up content that’s dynamically generated and not have to worry about the order in which your application processes to place it on your page. Both of these critical parts of web application development can now be achieved through Zend_Layout.
So, you’re probably wondering what horrific code I’m trying to get away from using. Imagine an application with code that looks like this:
Then, you’re actual page that’s supposed to do something looks like:
Or worse, suppose your file that actually does something looks like:
We can go further and show worse code samples than that, but I think you get the picture now, especially if you take into account an application where you have at least 25+ files all displaying data. Having to make sure that all of those files are 1) included at the top and bottom of your PHP files and 2) are in the correct order becomes a real big pain in the arse really quickly. Furthermore, the situation is complicated if you need to change the view based on certain criteria based in the processing section. You then have to do your processing first, and make sure that the data is returned rather than echo’ed straight to the response screen, or you have to buffer the output, capture the output and echo the buffer when rendering the view. It’s all very nasty, and I’ve come across my fair share of poorly implemented rendering for PHP applications.
So, how do we fix this? Well, I will spare the details of how an application needs to be setup for use within the Zend Framework (because it’s quite a detailed discussion). You’d be better off reading the Zend Framework documentation than reading what I’d have to say about it. The following is the only script like this in my software application. There’s no repitition of this code, and none of my scripts have to include it via require_once().
This view script is rendered in the echo $this->layout()->conent area. This facet of the Zend Framework makes it really powerful with respect to being able to completely change the look-and-feel of a web site without having to change anything but HTML. Furthermore, you can build in areas for plugins that are supposed to be rendered by doing something similar to the following:
The above is a small snippet of a layout. If it were a partial, though, that would still be acceptable and the full layout would render the partial.
In any case, this is just a small introduction to PHP Layouts. You’ll find more information about layouts in the Zend Framework Reference manual and the many different books written on the Zend Framework.