PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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 'Word_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 512. 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.

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

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

Example 513. 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.

<?php
$layout
->getInflector()->setTarget('layouts/:script.:suffix')
                       ->
setStaticRule('suffix''html')
                       ->
setFilterRule(array('Word_CamelCaseToUnderscore'));

Example 514. 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.

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

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 enableInflector() and disableInflector() methods.

Zend Framework