jump to navigation

Zend Framework : Managing the Layout April 20, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.

The most common problems that developers face is , HTML repetitive code . Not everything is dynamic content on a webpage , it can contain a lot of static content  , in the form of , headers , footers , meta-data , side bar menu’s . Zend layout utilizes a template file with place holders , and these place holders will contain wat ever dynamic content we want . The template file will contain all the static HTML code , this code will be broadcasted across the application and rendered with the dynamic content to produce the final result for the browser request .

Unfortunately , 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
This command will create a directory named layouts within your project’s application directory (webroot/application/layout/layout.phtml ) , 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> Hello this is a header </h1>

php  echo  $this->layout()->content  ;   ?>

<p> Copyright  by Some Brand Name </p>

In reality it is more complicated than this example . The layout will contain meta data , and the template will be constructed by  tables or even by CSS . My previous article demonstrated  a very simple Zend project where the layout where made with the help of tables .

Actually the Zend command line tool ( zf ) adds  also  an entry inside the applications central configuration file  (application/configs/application.ini)  in the form of :
resources.layout.layoutPath = APPLICATION_PATH “/layouts/scripts/” . The directory structure for the layout can be made manually , but then , the configuration file must also be updated .

The Zend Framework makes it posible  to use an alternative layout for a particular view, just crate a new layout file in the same directory as the previous layout file . Then within the controller action in which you’d like to use the new view, add this line :
$this->_helper_layout->setLayout(‘layout2’) ; //layout file name without the .phtml extension
If you’d like to disable a page’s layout and view altogether, use the following two lines to do so, respectively

$this->_helper->viewRenderer->setNoRender(true) ; //disabling the view
$this->_helper->layout()->disableLayout() ;   // disabling the layout

Further Reading : layout article on Zend


1. stuart - April 26, 2012

Thanks for the article. I learnt something about zf.

Do you know how to create a header and a footer menu using zend navigation?

tournasdimitrios1 - April 27, 2012

I’m glad that this article was helpful to you .
Zend’s navigation module deserves a separate article , which I’m plan to post soon .
A quick directive :
To use Zend Navigation do the following:

  • Add configuration entries for navigation in the application.ini file (specifying either the URI or Controller, Action and Module ).
  • Initialise and attach an instance of the Zend_Navigation container class to the navigation helper via the view in the bootstrap
  • Display the navigation bar with Zend_View_Helper_Navigation::menu() or the BreadCrumbs with Zend_View_Helper_Navigation::breadcrumbs() in your layout.phtml file.

  • Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s