Variable types
The eZ Publish template language supports the following variable types:
- Numbers
- Strings
- Booleans
- Arrays
- Objects
While some variable types can be created on the fly, others need to be created using an operator. Types that may be created directly are numbers and strings. Booleans and arrays must be created using operators, objects may be created using miscellaneous functions and operators. In addition to the types listed above, it is also possible to create and use custom variables. Custom variable types must be represented as objects.
Numbers
Numbers are numerical values. A number can be a positive or a negative integer or a floating point value. The following example demonstrates how different numbers can be used directly within template code:
{13} {1986} {3.1415} {102.5} {-1024} {-273.16}
Strings
A string is an arbitrary sequence of characters (text) that is encapsulated by a matching pair of either single or double quotes, ' or ". If the quotes are omitted, the string will most likely be interpreted as a function name. Strings are usually defined in the following way:
{'This is a string.'} {"This is another string."}
The output of the example above would be:
This is a string.
This is another a string.
Using quotes
It is possible to use quotes inside strings. This can be done by either using a different kind of quote or by making use of the escape character (backslash). The following examples demonstrate the use of quotes inside strings:
{'The following text is double quoted: "Rock and roll!" '} {"The following text is single quoted: 'Rock and roll!' "} {'Using both single and double quotes: "Rock\'n roll!" '} {'Using both single and double quotes: \'Rock\'n roll!\' '} {"Using both single and double quotes: 'Rock'n roll!' "} {"Using both single and double quotes: \"Rock'n roll\" "}
The output of the example above will be:
The following text is double quoted: "Rock and roll!"
The following text is single quoted: 'Rock and roll!'
Using both single and double quotes: "Rock'n roll!"
Using both single and double quotes: 'Rock'n roll!'
Using both single and double quotes: 'Rock'n roll!'
Using both single and double quotes: "Rock'n roll!"
Because of the way template code is defined (encapsulated in a matching pair of curly brackets), the right curly bracket, "}", must also be prepended by the backslash escape character. The following example demonstrates this.
{'{ This text is inside curly brackets.\}'}
The output of the template code above will be:
{This text is inside curly brackets.}
Template strings do not support inline expansion of variables (as in Perl and PHP). In other words, it is not possible to mix variables into strings. However, the concat operator can be used to append the contents of some variable to a string; which means that this operator can be used to build strings consisting of other strings and/or miscellaneous variables.
Booleans
Booleans are binary, they are either TRUE (1) or FALSE (0). A boolean must be created using either the "true" or the "false" template operator. Example:
{true()} {false()}
For some operators and functions, it is possible to use integers as booleans. However, these are not "real" booleans. Zero means FALSE; all non-zero values mean TRUE. Some operators are able to treat an array as if it were a boolean value. While an empty array means FALSE, a non-empty array means TRUE.
Arrays
Arrays are containers that are capable of holding a collection of any other variable type including other arrays. An array can be a simple vector or a hash map (associative array). An element of a vector can be accessed using an index number. The number denotes the position of the element inside the array (the first element is zero, the second element is one, and so on). An element of an associative array can be accessed using an identifier. Regular arrays can be created with the "array" operator. Associative arrays can be created with the hash operator. The following examples demonstrate the creation of arrays and hashes.
Example 1: Array of numbers
{array( 2, 4, 8, 16 )}
This example creates an array containing four numbers. The array will consist of the following elements:
Index | Value of element |
---|---|
0 |
2 |
1 |
4 |
2 |
8 |
3 |
16 |
Example 2: Array of strings
{array( 'This', 'is', 'a', 'test' )}
This example creates an array containing four strings. The array will consist of the following elements:
Index | Value |
---|---|
0 |
'This' |
1 |
'is' |
2 |
'a' |
3 |
'test' |
Example 3: Associative array
{hash( 'Red', 16, 'Green', 24, 'Blue', 32 )}
This example creates an associative array containing three key-value pairs. The array will consist of the following elements:
Key | Value |
---|---|
Red |
16 |
Green |
24 |
Blue |
32 |
Objects
Template objects are created by PHP code or by special template operators. The system uses objects to represent data structures of different kinds and sizes. For example, objects are used to represent information about content nodes, translations, webshop orders, user accounts, roles, policies and so on. Refer to the "Objects" section of the "Reference" chapter for a complete overview of the objects and their contents.
Object attributes
Objects consist of named attributes where each attribute can be a different type. The attributes may represent any type of data (numbers, strings, arrays, etc.) and even other objects. Since the attributes are named (each one has an identifier associated to it), their contents can be easily accessed using the different identifiers. This is done in the same way as when accessing the values of associative arrays using identifiers.
The following illustration shows the structure (with example values) of an object ("ezdate") that contains information about a date.
![The structure of the "ezdate" object.](../../../../../../var/doc/storage/images/ez_publish/technical_manual/3_10/images/templates/date_object/1367-3-eng-GB/date_object_doc.png)
The structure of the "ezdate" object.
The illustration above reveals that the "ezdate" object consists of five attributes ("timestamp", "is_valid", "year", "month" and "day"). All attributes are represented as strings except the "is_valid" attribute, which is a boolean. The values are the actual data that the object contains.
Attribute availability
It is worth noting that while some attributes are pre-fetched/calculated when an object itself is fetched, others are not. This means that accessing the contents of attributes may require additional processing (usually in the form of database queries). The "static" column in the reference documentation for objects indicates whether the different attributes provide pre-fetched values or if they need to be computed upon request. This information should be helpful when it comes to optimizing your templates.
Balazs Halasy (16/02/2005 9:15 am)
Svitlana Shatokhina (26/02/2008 3:13 pm)
Comments
There are no comments.