Previous Next

Zend_Layout Advanced Usage

Zend_Layout has a number of use cases for the advanced developer who wishes to adapt it for different view implementations, file system layouts, and more.

The major points of extension are:

  • Custom view objects. Zend_Layout allows you to utilize any class that implements Zend_View_Interface.

  • Custom front controller plugins. Zend_Layout ships with a standard front controller plugin that automates rendering of layouts prior to returning the response. You can substitute your own plugin.

  • Custom action helpers. Zend_Layout ships with a standard action helper that should be suitable for most needs as it is a dumb proxy to the layout object itself.

  • Custom layout script path resolution. Zend_Layout allows you to use your own inflector for layout script path resolution, or simply to modify the attached inflector to specify your own inflection rules.

Custom View Objects

Zend_Layout allows you to use any class implementing Zend_View_Interface or extending Zend_View_Abstract for rendering your layout script. Simply pass in your custom view object as a parameter to the constructor/startMvc(), or set it using the setView() accessor:

$view = new My_Custom_View();
$layout->setView($view);

Note: Not all Zend_View implementations are equal

While Zend_Layout allows you to use any class implementing Zend_View_Interface, you may run into issues if they can not utilize the various Zend_View helpers, particularly the layout and placeholder helpers. This is because Zend_Layout makes variables set in the object available via itself and placeholders.

If you need to use a custom Zend_View implementation that does not support these helpers, you will need to find a way to get the layout variables to the view. This can be done by either extending the Zend_Layout object and altering the render() method to pass variables to the view, or creating your own plugin class that passes them prior to rendering the layout.

Alternately, if your view implementation supports any sort of plugin capability, you can access the variables via the 'Zend_Layout' placeholder, using the placeholder helper:

$placeholders = new Zend_View_Helper_Placeholder();
$layoutVars   = $placeholders->placeholder('Zend_Layout')->getArrayCopy();

Custom Front Controller Plugins

When used with the MVC components, Zend_Layout registers a front controller plugin that renders the layout as the last action prior to exiting the dispatch loop. In most cases, the default plugin will be suitable, but should you desire to write your own, you can specify the name of the plugin class to load by passing the pluginClass option to the startMvc() method.

Any plugin class you write for this purpose will need to extend Zend_Controller_Plugin_Abstract, and should accept a layout object instance as an argument to the constructor. Otherwise, the details of your implementation are up to you.

The default plugin class used is Zend_Layout_Controller_Plugin_Layout.

Custom Action Helpers

When used with the MVC components, Zend_Layout registers an action controller helper with the helper broker. The default helper, Zend_Layout_Controller_Action_Helper_Layout, acts as a dumb proxy to the layout object instance itself, and should be suitable for most use cases.

Should you feel the need to write custom functionality, simply write an action helper class extending Zend_Controller_Action_Helper_Abstract and pass the class name as the helperClass option to the startMvc() method. Details of the implementation are up to you.

Custom Layout Script Path Resolution: Using the Inflector

Zend_Layout uses Zend_Filter_Inflector to establish a filter chain for translating a layout name to a layout script path. By default, it uses the rules 'CamelCaseToDash' followed by 'StringToLower', and the suffix 'phtml' to transform the name to a path. As some examples:

  • 'foo' will be transformed to 'foo.phtml'.

  • 'FooBarBaz' will be transformed to 'foo-bar-baz.phtml'.

You have three options for modifying inflection: modify the inflection target and/or view suffix via Zend_Layout accessors, modify the inflector rules and target of the inflector associated with the Zend_Layout instance, or create your own inflector instance and pass it to Zend_Layout::setInflector().

Example #1 Using Zend_Layout accessors to modify the inflector

The default Zend_Layout inflector uses static references for the target and view script suffix, and has accessors for setting these values.

// Set the inflector target:
$layout->setInflectorTarget('layouts/:script.:suffix');

// Set the layout view script suffix:
$layout->setViewSuffix('php');

Example #2 Direct modification of Zend_Layout inflector

Inflectors have a target and one or more rules. The default target used with Zend_Layout is ':script.:suffix'; ':script' is passed the registered layout name, while ':suffix' is a static rule of the inflector.

Let's say you want the layout script to end in the suffix 'html', and that you want to separate MixedCase and camelCased words with underscores instead of dashes, and not lowercase the name. Additionally, you want it to look in a 'layouts' subdirectory for the script.

$layout->getInflector()->setTarget('layouts/:script.:suffix')
                       ->setStaticRule('suffix', 'html')
                       ->setFilterRule(array('CamelCaseToUnderscore'));

Example #3 Custom inflectors

In most cases, modifying the existing inflector will be enough. However, you may have an inflector you wish to use in several places, with different objects of different types. Zend_Layout supports this.

$inflector = new Zend_Filter_Inflector('layouts/:script.:suffix');
$inflector->addRules(array(
    ':script' => array('CamelCaseToUnderscore'),
    'suffix'  => 'html'
));
$layout->setInflector($inflector);

Note: Inflection can be disabled

Inflection can be disabled and enabled using accessors on the Zend_Layout object. This can be useful if you want to specify an absolute path for a layout view script, or know that the mechanism you will be using for specifying the layout script does not need inflection. Simply use the enableInflection() and disableInflection() methods.

Previous Next
Introduction to Zend Framework
Overview
Installation
Zend_Acl
Introduction
Refining Access Controls
Advanced Usage
Zend_Amf
Introduction
Zend_Amf_Server
Zend_Auth
Introduction
Database Table Authentication
Digest Authentication
HTTP Authentication Adapter
LDAP Authentication
Open ID Authentication
Zend_Cache
Introduction
The theory of caching
Zend_Cache frontends
Zend_Cache backends
Zend_Captcha
Introduction
Captcha Operation
Captcha Adapters
Zend_Config
Introduction
Theory of Operation
Zend_Config_Ini
Zend_Config_Xml
Zend_Config_Writer
Zend_Config_Writer
Zend_Console_Getopt
Introduction to Getopt
Declaring Getopt Rules
Fetching Options and Arguments
Configuring Zend_Console_Getopt
Zend_Controller
Zend_Controller Quick Start
Zend_Controller Basics
The Front Controller
The Request Object
The Standard Router
The Dispatcher
Action Controllers
Action Helpers
The Response Object
Plugins
Using a Conventional Modular Directory Structure
MVC Exceptions
Migrating from Previous Versions
Zend_Currency
Introduction to Zend_Currency
How to work with currencies
Migrating from Previous Versions
Zend_Date
Introduction
Theory of Operation
Basic Methods
Zend_Date API Overview
Creation of dates
Constants for General Date Functions
Working examples
Zend_Db
Zend_Db_Adapter
Zend_Db_Statement
Zend_Db_Profiler
Zend_Db_Select
Zend_Db_Table
Zend_Db_Table_Row
Zend_Db_Table_Rowset
Zend_Db_Table Relationships
Zend_Debug
Dumping Variables
Zend_Dojo
Introduction
Zend_Dojo_Data: dojo.data Envelopes
Dojo View Helpers
Dojo Form Elements and Decorators
Zend_Dom
Introduction
Zend_Dom_Query
Zend_Exception
Using Exceptions
Zend_Feed
Introduction
Importing Feeds
Retrieving Feeds from Web Pages
Consuming an RSS Feed
Consuming an Atom Feed
Consuming a Single Atom Entry
Modifying Feed and Entry structures
Custom Feed and Entry Classes
Zend_File
Zend_File_Transfer
Validators for Zend_File_Transfer
Filters for Zend_File_Transfer
Migrating from previous versions
Zend_Filter
Introduction
Standard Filter Classes
Filter Chains
Writing Filters
Zend_Filter_Input
Zend_Filter_Inflector
Zend_Form
Zend_Form
Zend_Form Quick Start
Creating Form Elements Using Zend_Form_Element
Creating Forms Using Zend_Form
Creating Custom Form Markup Using Zend_Form_Decorator
Standard Form Elements Shipped With Zend Framework
Standard Form Decorators Shipped With Zend Framework
Internationalization of Zend_Form
Advanced Zend_Form Usage
Zend_Gdata
Introduction to Gdata
Authenticating with AuthSub
Using the Book Search Data API
Authenticating with ClientLogin
Using Google Calendar
Using Google Documents List Data API
Using Google Health
Using Google Spreadsheets
Using Google Apps Provisioning
Using Google Base
Using Picasa Web Albums
Using the YouTube Data API
Catching Gdata Exceptions
Zend_Http
Zend_Http_Client - Introduction
Zend_Http_Client - Advanced Usage
Zend_Http_Client - Connection Adapters
Zend_Http_Cookie and Zend_Http_CookieJar
Zend_Http_Response
Zend_InfoCard
Introduction
Zend_Json
Introduction
Basic Usage
JSON Objects
XML to JSON conversion
Zend_Json_Server - JSON-RPC server
Zend_Layout
Introduction
Zend_Layout Quick Start
Zend_Layout Configuration Options
Zend_Layout Advanced Usage
Zend_Ldap
Introduction
Zend_Loader
Loading Files and Classes Dynamically
Loading Plugins
Zend_Locale
Introduction
Using Zend_Locale
Normalization and Localization
Working with Dates and Times
Supported locales
Migrating from previous versions
Zend_Log
Overview
Writers
Formatters
Filters
Zend_Mail
Introduction
Sending via SMTP
Sending Multiple Mails per SMTP Connection
Using Different Transports
HTML E-Mail
Attachments
Adding Recipients
Controlling the MIME Boundary
Additional Headers
Character Sets
Encoding
SMTP Authentication
Securing SMTP Transport
Reading Mail Messages
Zend_Measure
Introduction
Creation of Measurements
Outputting measurements
Manipulating Measurements
Types of measurements
Zend_Memory
Overview
Memory Manager
Memory Objects
Zend_Mime
Zend_Mime
Zend_Mime_Message
Zend_Mime_Part
Zend_OpenId
Introduction
Zend_OpenId_Consumer Basics
Zend_OpenId_Provider
Zend_Paginator
Introduction
Usage
Configuration
Advanced usage
Zend_Pdf
Introduction.
Creating and loading PDF documents.
Save changes to the PDF document.
Document pages.
Drawing
Document Info and Metadata.
Zend_Pdf module usage example
Zend_ProgressBar
Zend_ProgressBar
Zend_Registry
Using the Registry
Zend_Rest
Introduction
Zend_Rest_Client
Zend_Rest_Server
Zend_Search_Lucene
Overview
Building Indexes
Searching an Index
Query Language
Query Construction API
Character Set
Extensibility
Interoperating with Java Lucene
Advanced
Best Practices
Zend_Server
Introduction
Zend_Server_Reflection
Zend_Service
Introduction
Zend_Service_Akismet
Zend_Service_Amazon
Zend_Service_Audioscrobbler
Zend_Service_Delicious
Zend_Service_Flickr
Zend_Service_Nirvanix
Zend_Service_ReCaptcha
Zend_Service_Simpy
Introduction
Zend_Service_StrikeIron
Zend_Service_StrikeIron: Bundled Services
Zend_Service_StrikeIron: Advanced Uses
Zend_Service_Technorati
Zend_Service_Twitter
Zend_Service_Yahoo
Zend_Session
Introduction
Basic Usage
Advanced Usage
Global Session Management
Zend_Session_SaveHandler_DbTable
Zend_Soap
Zend_Soap_Server
Zend_Soap_Client
WSDL Accessor
AutoDiscovery
Zend_Test
Introduction
Zend_Test_PHPUnit
Zend_Text
Zend_Text_Figlet
Zend_Text_Table
Zend_TimeSync
Introduction
Working with Zend_TimeSync
Zend_Translate
Introduction
Adapters for Zend_Translate
Using Translation Adapters
Migrating from previous versions
Zend_Uri
Zend_Uri
Zend_Validate
Introduction
Standard Validation Classes
Validator Chains
Writing Validators
Zend_Version
Reading the Zend Framework Version
Zend_View
Introduction
Controller Scripts
View Scripts
View Helpers
Zend_View_Abstract
Zend_Wildfire
Zend_Wildfire
Zend_XmlRpc
Introduction
Zend_XmlRpc_Client
Zend_XmlRpc_Server
Zend Framework Requirements
PHP Version
PHP Extensions
Zend Framework Components
Zend Framework Dependencies
Zend Framework Coding Standard for PHP
Overview
PHP File Formatting
Naming Conventions
Coding Style
Zend Framework Performance Guide
Introduction
Class Loading
Internationalization (i18n) and Localization (l10n)
View Rendering
Copyright Information