#native_company# #native_desc#
#native_cta#

Managing Zend Framework Layouts Page 2

By W. Jason Gilmore
on May 20, 2010

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:

echo $this->_helper->json($this->results);

Incidentally, the JSON helper will also automatically disable the layout and view for the action, saving you from having to execute the aforementioned disableLayout() and setNoRender() methods. Because most browsers do not recognize the application/json content 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.

Conclusion

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

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
.”