TracNav menu
Variable Modifiers
Variable modifiers give power to the template designer. Assume you pass a very long text to a variable but in the layout the designer only has space for a short teaser text. Using modifers, the designer may apply a Truncate modifier that shortens the text you pass in your PHP script. And this is only one application for variable modifiers.
Introduction
Applying modifers is really a piece of cake. All you have to do is to use the <patTemplate:var/> tag instead of only enclosing the variable in { and }. Then you may use the 'modifier' attribute to set the modifier.
<patTemplate:tmpl name="page">
<div>
<patTemplate:var name="myVar" modifier="nl2br"/>
</div>
</patTemplate:tmpl>
Variable modifiers will be applied to the value of a variable when parsing the template. There two types of modfifiers:
- You may use any PHP function as a variable modifier. The modifier must accept only a string as a parameter and also return a string. Perfect examples for variable modifiers are nl2br() or htmlentities().
- You may create custom modifiers, which provide extended functionality and which may be influenced by the attributes of the var tag.
Custom Modifiers
Custom Modifiers are subclasses of patTemplate_Modifier and have to be placed inside patTemplate/Modifier. The filename has to match the last part of the classname. That means if you would like to create a filter that truncates sentences you will have to create a new class 'patTemplate_Modifier_Truncate' and place it into 'patTemplate/Modifier/Truncate.php'. The class has to implement one method modify( string value, array params ) that has to return the modified value. You may do whatever you like inside this method to modify the value. patTemplate will pass two parameters to your method:
- string $value, the value of the variable
- array $params, an associative array containing all attributes of the var tag except the ones, that are used internally (name, default, copyFrom, modifier)
The truncate modifier could look like this:
<?PHP
/**
* patTemplate modfifier Truncate
*
* $Id: modifiers.xml 34 2004-05-11 19:46:09Z schst $
*
* @package patTemplate
* @subpackage Modifiers
* @author Stephan Schmidt <schst@php.net>
*/
class patTemplate_Modifier_Truncate extends patTemplate_Modifier
{
/**
* truncate the string
*
* @access public
* @param string value
* @return string modified value
*/
function modify( $value, $params = array() )
{
/**
* no length specified
*/
if( !isset( $params['length'] ) )
return $value;
/**
* is shorter than the length
*/
if( $params['length'] > strlen( $value ) )
return $value;
return substr( $value, 0, $params['length'] ) . '...';
}
}
?>
If you copy this file to the Modifier folder, you may instantly use it in your template files, by specifying it in the modifier attribute of any <patTemplate:var/> tag.
<patTemplate:tmpl name="page"> <div> <patTemplate:var name="myVar" modifier="Truncate" length="50"/> </div> </patTemplate:tmpl>
If you now pass a value that is longer than 50 chars, the modifier will automatically truncate it, before inserting it into the template.
