Programmer's Reference Guide

Create Your Project

Create A Layout

You may have noticed that the view scripts in the previous sections were HTML fragments- not complete pages. This is by design; we want our actions to return content only related to the action itself, not the application as a whole.

Now we must compose that generated content into a full HTML page. We'd also like to have a consistent look and feel for the application. We will use a global site layout to accomplish both of these tasks.

There are two design patterns that Zend Framework uses to implement layouts: » Two Step View and » Composite View. Two Step View is usually associated with the » Transform View pattern; the basic idea is that your application view creates a representation that is then injected into the master view for final transformation. The Composite View pattern deals with a view made of one or more atomic, application views.

In Zend Framework, Zend_Layout combines the ideas behind these patterns. Instead of each action view script needing to include site-wide artifacts, they can simply focus on their own responsibilities.

Occasionally, however, you may need application-specific information in your site-wide view script. Fortunately, Zend Framework provides a variety of view placeholders to allow you to provide such information from your action view scripts.

To get started using Zend_Layout, first we need to inform our bootstrap to use the Layout resource. This can be done using the zf enable layout command:

  1. % zf enable layout
  2. Layouts have been enabled, and a default layout created at
  3. application/layouts/scripts/layout.phtml
  4. A layout entry has been added to the application config file.

As noted by the command, application/configs/application.ini is updated, and now contains the following within the production section:

  1. ; application/configs/application.ini
  2.  
  3. ; Add to [production] section:
  4. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

The final INI file should look as follows:

  1. ; application/configs/application.ini
  2.  
  3. [production]
  4. ; PHP settings we want to initialize
  5. phpSettings.display_startup_errors = 0
  6. phpSettings.display_errors = 0
  7. includePaths.library = APPLICATION_PATH "/../library"
  8. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  9. bootstrap.class = "Bootstrap"
  10. appnamespace = "Application"
  11. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  12. resources.frontController.params.displayExceptions = 0
  13. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  14.  
  15. [staging : production]
  16.  
  17. [testing : production]
  18. phpSettings.display_startup_errors = 1
  19. phpSettings.display_errors = 1
  20.  
  21. [development : production]
  22. phpSettings.display_startup_errors = 1
  23. phpSettings.display_errors = 1

This directive tells your application to look for layout view scripts in application/layouts/scripts. If you examine your directory tree, you'll see that this directory has been created for you now, with the file layout.phtml.

We also want to ensure we have an XHTML DocType declaration for our application. To enable this, we need to add a resource to our bootstrap.

The simplest way to add a bootstrap resource is to simply create a protected method beginning with the phrase _init. In this case, we want to initialize the doctype, so we'll create an _initDoctype() method within our bootstrap class:

  1. // application/Bootstrap.php
  2.  
  3. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  4. {
  5.     protected function _initDoctype()
  6.     {
  7.     }
  8. }

Within that method, we need to hint to the view to use the appropriate doctype. But where will the view object come from? The easy solution is to initialize the View resource; once we have, we can pull the view object from the bootstrap and use it.

To initialize the view resource, add the following line to your application/configs/application.ini file, in the section marked production:

  1. ; application/configs/application.ini
  2.  
  3. ; Add to [production] section:
  4. resources.view[] =

This tells us to initialize the view with no options (the '[]' indicates that the "view" key is an array, and we pass nothing to it).

Now that we have a view, let's flesh out our _initDoctype() method. In it, we will first ensure the View resource has run, fetch the view object, and then configure it:

  1. // application/Bootstrap.php
  2.  
  3. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  4. {
  5.     protected function _initDoctype()
  6.     {
  7.         $this->bootstrap('view');
  8.         $view = $this->getResource('view');
  9.         $view->doctype('XHTML1_STRICT');
  10.     }
  11. }

Now that we've initialized Zend_Layout and set the Doctype, let's create our site-wide layout:

  1. <!-- application/layouts/scripts/layout.phtml -->
  2. <?php echo $this->doctype() ?>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.   <title>Zend Framework Quickstart Application</title>
  7.   <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
  8. </head>
  9. <body>
  10. <div id="header" style="background-color: #EEEEEE; height: 30px;">
  11.     <div id="header-logo" style="float: left">
  12.         <b>ZF Quickstart Application</b>
  13.     </div>
  14.     <div id="header-navigation" style="float: right">
  15.         <a href="<?php echo $this->url(
  16.             array('controller'=>'guestbook'),
  17.             'default',
  18.             true) ?>">Guestbook</a>
  19.     </div>
  20. </div>
  21.  
  22. <?php echo $this->layout()->content ?>
  23.  
  24. </body>
  25. </html>

We grab our application content using the layout() view helper, and accessing the "content" key. You may render to other response segments if you wish to, but in most cases, this is all that's necessary.

Note also the use of the headLink() placeholder. This is an easy way to generate the HTML for <link> elements, as well as to keep track of them throughout your application. If you need to add additional CSS sheets to support a single action, you can do so, and be assured it will be present in the final rendered page.

Note: Checkpoint
Now go to "http://localhost" and check out the source. You should see your XHTML header, head, title, and body sections.


Create Your Project

Comments

I have problem with this implementation, if i try to copy all like here and try browser.. i ll get this

Message: Invalid controller specified (guestbook)


Request Parameters:
array (
'controller' => 'guestbook',
'action' => 'index',
'module' => 'default',
)

there is some problem with controller, but i'm trying to do web app first time, and its to hard to handle this problem .. pls help..
I installed the Zend Framework via Zend Server CE and found out that the "zf enable layout" command didn't work.

The problem was on the version of the Zend Framework in Zend Server CE. I've downloaded the latest release of ZF and extracted it to C:\Program Files\Zend\ZendServer\share\ZendFramework and it solved the problem
@Ondrej Nedvidek

That only happens when you click the "Guestbook" hyperlink at the top right portion of the webpage, right?

That's because the guestbook controller was not created yet, check out the next of this guide:

http://framework.zend.com/manual/en/learning.quickstart.cre
When trying to create a layout using the zf cli I obtain:
An Error Has Occurred
Action 'enable' is not a valid action.
Hi, having the same problem with the zf enable layout command... anyone has found the solution, I know Joan did, but my question is did you have to reinstall the who Zend Server CE?

Thanks
A much quicker way to add doctype is to skip the Bootstrap.php file explained above, skip the resources.view[] =
line and just add this to application.ini:
resources.view.doctype = "XHTML1_STRICT"
While running the "zf enable layout" command, it through an following error.

" An Error Has Occurred
A project profile was not found.

Zend Framework Command Line Console Tool v1.10.2
Details for action "Enable" and provider "Layout"
Layout
zf enable layout"

Please help me?
@Mahesh

You mast be inside your project folder and than run this command


I have different problem. After enabling layout I get Internal Server Error. Why?

+ Add A Comment

If you have a JIRA/Crowd account, we suggest you login first before commenting.
  • BBCode is allowed in the comment markup


  • Select a Version

    Languages Available

    Components

    Search the Manual