One immediate benefit developers stand to gain from adopting a framework such as the Zend Framework is a final resolution on managing website layouts and pages. Gone is the need to devise strategies for important tasks such as maintaining page headers and footers, separating the bulk of a page’s logic from its interface, and managing the code repeatedly used throughout the site to carry out special formatting actions. Instead, you can just embrace the framework’s conventions and move on to the next battle. This article introduces you to some of the fundamental concepts behind managing layouts within your Zend Framework-driven applications.
A website’s layout can be thought of as its template, which is to say the parts of the site design that generally do not change as the user navigates from one page to the next. Many PHP developers employ an approach involving managing page headers and footers in separate files and then using
require()statements to assemble the page. While this approach may be fine for simple sites, it quickly becomes a burden as the site grows in complexity. The Zend Framework greatly improves upon this approach by utilizing a single template file (known as the layout), which is dynamically wrapped around the site’s pages.
Enabling Layout Management
Oddly, the Zend Framework’s layout feature isn’t enabled by default. To enable this feature you’ll need to navigate to your application’s home directory and use the Zend_Tool utility:
%>zf enable layout
Executing this command will create a directory named
layoutswithin your project’s
applicationdirectory, and within it, a file named
layout.phtml. Open this file and you’ll see just a single line:
<?php echo $this->layout()->content; ?>
This line represents the location within the template where the content that makes up your site’s various pages (views) will be injected. Therefore, to add a header and footer to your site template all you need to do is code around this line, for instance:
<h1>Welcome to Acme, Inc.</h1> <?php echo $this->layout()->content; ?> <p> Copyright © 2010 Acme, Inc. </p>
If you’d like to use an alternative layout for a particular view, copy the
layout.phtmlfile and modify it to suit the design of your new template, before saving it under a new name within the
layoutsdirectory. Then within the controller action in which you’d like to use the new view, add this line:
Be sure to replace
layout2with the name of your new template. If you’d like to use this template in conjunction with all actions found in a specific controller, just place the above line in your controller’s
If you’d like to disable a page’s layout and view altogether, use the following two lines to do so, respectively:
Using Layout Helpers
The Zend Framework is bundled with numerous helpers, which can be used to set and override layout attributes. For instance, if you were creating a web service that specified that JSON would be exclusively returned with each request, then within the controller you should set the content type to
application/json. To do this all you need to do is return the JSON using the JSON helper:
Incidentally, the JSON helper will also automatically disable the layout and view for the action, saving you from having to execute the aforementioned
setNoRender()methods. Because most browsers do not recognize the
application/jsoncontent type as a format that can be displayed within the browser, attempts to navigate to the URL associated with this action will cause the browser to prompt you for further instruction regarding what to do with the returned file. Of course, because the intent of such an action is to respond to AJAX requests, browser-specific behavior of this sort is irrelevant.
Many other native view helpers exist, consult the Zend Framework documentation for more information.
As I’ve mentioned many times within these pages, frameworks greatly improve developer productivity thanks to the “convention over configuration” paradigm. By reducing the number of decisions developers need to make in terms of implementation approach, additional time can logically be spent focusing on creating compelling features rather than, say, worrying about how to manage configuration data. Further, because the convention implementations make it easy (although not required) for the developer to employ widely accepted industry best practices such as database abstraction and data validation, framework adoption comes with the added bonus of producing more maintainable and secure code.
The Zend Framework offers users a great degree of layout flexibility, supporting the ability to create a global template, switch between multiple templates, override template defaults such as the document type, and even disable templates altogether. Do you have any useful layout-related tips or tricks? Tell us about them in the comments!
About the Author (copied post from this author thanks for this nice tut)
Jason Gilmore is the founder of EasyPHPWebsites.com and the author of several popular books, including “Easy PHP Web sites with the Zend Framework” and “Beginning PHP and MySQL: From Novice to Professional” (currently in its third edition). Check out his new DZone reference card, titled “Getting Started with the Zend Framework.”