Attributes
Attributes are a way of accessing data in objects and arrays.
The attribute is an identifier that will lookup either the index number or the key in an array
or the defined attribute in an object. The access method for arrays and objects are similar
which means that the template code can be fed an array or an object and the code wouldn't know the
difference.
Arrays
Array elements have two types of attributes, numerical indices or identifiers.
Index lookup
{$array.1}
{$array.24}
Identifier lookup
{$array.first}
{$array.a.b.c.d.e.f}
Objects
For objects to be able to handle attribute access the class must implement these following functions.
- bool hasAttribute( string name )
- mixed &attribute( string name )
It also recommended, but not necessary, to implement these functions.
- array attributes()
- setAttribute( string name, mixed value )
Example
Here's an example of how to implement attribute support.
class eZItem
{
function attributes()
{
return array( "first", "second" );
}
function hasAttribute( $name )
{
return in_array( $name, eZItem::attributes() );
}
function &attribute;( $name )
{
if ( $name == "first" )
return $this->First;
else if ( $name == "second" )
return $this->Second;
return null;
}
function setAttribute( $name, $value )
{
if ( $name == "first" )
$this->First = $value;
else if ( $name == "second" )
$this->Second = $value;
}
var $First;
var $Second;
}
Access methods
It's possible to access attributes using two different methods, this is shown below. The first uses the
. operator and an identifier name or numerical index. The second uses the [ and ]
brackets to enclose a new statement for which the result is used to do the lookup.
. operator
{$array.1}
{$array.first.last}
[] operator
{$array[1]}
{$array[first][last]}
It's also possible to use another variable as lookup, doing this is best suited with the [] brackets.
{$array.$index}
{$array[$iterator.key]}
|