PHPBuilder - Managing Zend Framework Layouts



RSS Twitter
Articles Html

Managing Zend Framework Layouts

by: W. Jason Gilmore
|
May 20, 2010

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.

Managing Layouts

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 layouts within your project's application directory, 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 &copy; 2010 Acme, Inc.
</p>

Switching Layouts

If you'd like to use an alternative layout for a particular view, copy the layout.phtml file and modify it to suit the design of your new template, before saving it under a new name within the layouts directory. Then within the controller action in which you'd like to use the new view, add this line:
$this->_helper_layout->setLayout('layout2');
Be sure to replace layout2 with 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 init() method.

Disabling Layouts

If you'd like to disable a page's layout and view altogether, use the following two lines to do so, respectively:
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

1
|
2
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
W. Jason Gilmore

Comment:



Comment:

(Maximum characters: 1200). You have characters left.