Introduction | Getting Started with Zend_Layout Programmer's Reference Guide | Zend_Layout: Conclusions

# Using Zend_Layout

Basic usage of Zend_Layout is fairly trivial. Assuming you're using Zend_Application already, you can simply provide some configuration options and create a layout view script.

# Layout Configuration

The recommended location of layouts is in a "layouts/scripts/" subdirectory of your application:

1. application
2. |-- Bootstrap.php
3. |-- configs
4. | `-- application.ini
5. |-- controllers
6. |-- layouts
7. | `-- scripts
8. | |-- layout.phtml

To initialize Zend_Layout, add the following to your configuration file ("application/configs/application.ini"):

1. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
2. resources.layout.layout = "layout"

The first line indicates where to look for layout scripts; the second line gives the name of the layout to use, minus the view script extension (which is assumed to be ".phtml" by default).

# Create a Layout Script

Now that you have your configuration in place, you need to create your layout script. First, make sure that you've created the "application/layouts/scripts" directory; then, open an editor, and create the markup for your layout. Layout scripts are simply view scripts, with some slight differences.

1. <html>
2. <head>
3. <title>My Site</title>
4. </head>
5. <body>
6. <?php echo $this->layout()->content ?>
7. </body>
8. </html>

In the example above, you'll note the call to a layout() view helper. When you register the Zend_Layout resource, you also gain access to both an action and view helper that allow you access to the Zend_Layout instance; you can then call operations on the layout object. In this case, we're retrieving a named variable, $content, and echoing it. By default, the $content variable is populated for you from the application view script rendered. Otherwise, anything you'd normally do in a view script is perfectly valid -- call any helpers or view methods you desire.

At this point, you have a working layout script, and your application is informed of its location and knows to render it.

# Accessing the Layout Object

On occasion, you may need direct access to the layout object. There are three ways you can do this:

- Within view scripts: use the layout() view helper, which returns the Zend_Layout instance registered with the front controller plugin.

  1. <?php $layout = $this->layout(); ?>

  Since it returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.

- Within action controllers: use the layout() action helper, which acts just like the view helper.

  1. // Calling helper as a method of the helper broker:
  2. $layout = $this->_helper->layout();
  4. // Or, more verbosely:
  5. $helper = $this->_helper->getHelper('Layout');
  6. $layout = $helper->getLayoutInstance();

  As with the view helper, since the action helper returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.

- Elsewhere: use the static method getMvcInstance(). This will return the layout instance registered by the bootstrap resource.

  1. $layout = Zend_Layout::getMvcInstance();

- Via the bootstrap: retrieve the layout resource, which will be the Zend_Layout instance.

  1. $layout = $bootstrap->getResource('Layout');

  Anywhere you have access to the bootstrap object, this method is preferred over using the static getMvcInstance() method.

# Other Operations

In most cases, the above configuration and layout script (with modifications) will get you what you need. However, some other functionality exists you will likely use sooner or later. In all of the following examples, you may use one of the methods listed above for retrieving the layout object.

- Setting layout variables. Zend_Layout keeps its own registry of layout-specific view variables that you can access; the $content key noted in the initial layout script sample is one such example. You can assign and retrieve these using normal property access, or via the assign() method.

  1. // Setting content:
  2. $layout->somekey = "foo"
  4. // Echoing that same content:
  5. echo $layout->somekey; // 'foo'
  7. // Using the assign() method:
  8. $layout->assign('someotherkey', 'bar');
  10. // Access to assign()'d variables remains the same:
  11. echo $layout->someotherkey; // 'bar'

- disableLayout(). Occasionally, you may want to disable layouts; for example, when answering an Ajax request, or providing a RESTful representation of a resource. In these cases, you can call the disableLayout() method on your layout object.

  1. $layout->disableLayout();

  The opposite of this method is, of course, enableLayout(), which can be called at any time to re-enable layouts for the requested action.

- Selecting an alternate layout: If you have multiple layouts for your site or application, you can select the layout to use at any time by simply calling the setLayout() method. Call it by specifying the name of the layout script without the file suffix.

  1. // Use the layout script "alternate.phtml":
  2. $layout->setLayout('alternate');

  The layout script should reside in the $layoutPath directory specified in your configuration. Zend_Layout will then use this new layout when rendering.

Introduction | Getting Started with Zend_Layout Programmer's Reference Guide | Zend_Layout: Conclusions