Zend_Layout Quick Start

Zend_Layout Configuration Options

Zend_Layout has a variety of configuration options. These may be set by calling the appropriate accessors, passing an array or Zend_Config object to the constructor or startMvc(), passing an array of options to setOptions(), or passing a Zend_Config object to setConfig().

  • layout: the layout to use. Uses the current inflector to resolve the name provided to the appropriate layout view script. By default, this value is 'layout' and resolves to 'layout.phtml'. Accessors are setLayout() and getLayout().

  • layoutPath: the base path to layout view scripts. Accessors are setLayoutPath() and getLayoutPath().

  • contentKey: the layout variable used for default content (when used with the MVC). Default value is 'content'. Accessors are setContentKey() and getContentKey().

  • mvcSuccessfulActionOnly: when using the MVC, if an action throws an exception and this flag is TRUE, the layout will not be rendered (this is to prevent double-rendering of the layout when the ErrorHandler plugin is in use). By default, the flat is TRUE. Accessors are setMvcSuccessfulActionOnly() and getMvcSuccessfulActionOnly().

  • view: the view object to use when rendering. When used with the MVC, Zend_Layout will attempt to use the view object registered with the ViewRenderer if no view object has been passed to it explicitly. Accessors are setView() and getView().

  • helperClass: the action helper class to use when using Zend_Layout with the MVC components. By default, this is Zend_Layout_Controller_Action_Helper_Layout. Accessors are setHelperClass() and getHelperClass().

  • pluginClass: the front controller plugin class to use when using Zend_Layout with the MVC components. By default, this is Zend_Layout_Controller_Plugin_Layout. Accessors are setPluginClass() and getPluginClass().

  • inflector: the inflector to use when resolving layout names to layout view script paths; see the Zend_Layout inflector documentation for more details. Accessors are setInflector() and getInflector().

Note: helperClass and pluginClass must be passed to startMvc()
In order for the helperClass and pluginClass settings to have effect, they must be passed in as options to startMvc(); if set later, they have no affect.

Examples

The following examples assume the following $options array and $config object:

  1. span style="color: #ff0000;">'layout'     => 'foo',
  2.     'layoutPath' => '/path/to/layouts',
  3.     'contentKey' => 'CONTENT',           // ignored when MVC not used
  4. );
  1. /**
  2. [layout]
  3. layout = "foo"
  4. layoutPath = "/path/to/layouts"
  5. contentKey = "CONTENT"
  6. */'/path/to/layout.ini', 'layout');

Example #1 Passing options to the constructor or startMvc()

Both the constructor and the startMvc() static method can accept either an array of options or a Zend_Config object with options in order to configure the Zend_Layout instance.

First, let's look at passing an array:

  1. // Using constructor:
  2. // Using startMvc():

And now using a config object:

  1. span style="color: #ff0000;">'/path/to/layout.ini', 'layout');
  2.  
  3. // Using constructor:
  4. // Using startMvc():

Basically, this is the easiest way to customize your Zend_Layout instance.

Example #2 Using setOption() and setConfig()

Sometimes you need to configure the Zend_Layout object after it has already been instantiated; setOptions() and setConfig() give you a quick and easy way to do so:

  1. // Using an array of options:
  2. $layout->setOptions($options);
  3.  
  4. // Using a Zend_Config object:
  5. $layout->setConfig($options);

Note, however, that certain options, such as pluginClass and helperClass, will have no affect when passed using this method; they need to be passed to the constructor or startMvc() method.

Example #3 Using Accessors

Finally, you can also configure your Zend_Layout instance via accessors. All accessors implement a fluent interface, meaning their calls may be chained:

  1. $layout->setLayout('foo')
  2.        ->setLayoutPath('/path/to/layouts''CONTENT');

Zend_Layout Quick Start