ez.no / ezpublish / documentation / customization / custom design / variables, arrays and objects
These documentation pages are no longer maintained. Please visit the new documentation site.
Variables can be set with either let or default. let will always assign the value, but default will only set the variable if it is not already set, i.e. it will not override an existing variable.
Example:
{let myvalue=5} My value: {$myvalue} {/let}
Output:
My value: 5
Notice that both let and default should be closed with a corresponding {/let} or {/default} tag. Our variable will no longer be defined after the closing.
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.
Array elements have two types of attributes, numerical indices or identifiers.
{$array.1} {$array.24}
{$array.first} {$array.a.b.c.d.e.f}
For objects to be able 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 )
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; }
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.
{$array.1} {$array.first.last}
{$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]}
Comments