29-11-2011 : Remettre dans le débat politique les principes du Conseil National de la Résistance
17-02-2012 : Pétition pour une protection de l’apiculture et des consommateurs face au lobby des OGM
Logo de mon site
Logo de mon site
Faire un don




Right menu

Logo du site ez.no  Logo XHTML 1.O du W3C  Logo XHTML 1.O du W3C  Site francophone officiel de Firefox
zero papier grâce aux catalogues et promos en ligne de bonial

The template language -- Langage des templates

Table des matières

  1. The template language -- Langage des templates
  2. Comments -- Commentaires
  3. Variable types -- Types de variables
  4. Variable usage -- Utilisation des variables
  5. Array and object inspection -- Inspection des tableaux et objets
  6. Control structures -- Structures de contrôle
  7. Functions and operators -- Fonctions et opérateurs

Variable usage -- Utilisation des variables

Date de publication: le mardi 29 mars 2011 à 20h39
Dernière modification: par Pascal BOYER le lundi 4 avril 2011 à 20h55

28/09/2010 1:21

versions 3.9, 3.10, 4.x

Template variables must be referenced using dollar ($) notation, for example: $my_variable, $object_array, etc. An eZ Publish template variable is case sensitive. In other words, $lollipop is not the same variable as $LolliPop. Template variables can be created by the system (from PHP code) or by the author of the template (from within template code). Regardless of where a variable was created, it can be changed using the "set" function. Some templates have preset variables, for example, the main template (pagelayout) provides access to a collection of variables.
Les variables de template doivent être référencées par la notation dollar ($), comme par exemple: $my_variable, $object_array, etc... Une variable de template eZ Publish est sensible à la casse, ce qui signifie que $lollipop n'est pas la même chose que $LolliPop. Les variables de template peuvent être créées par le système (à partir de code PHP) ou bien par l'auteur d'un template (à partir du code même du template). Indépendamment de l'endroit où est créée la variable, sa valeur peut être modifiée en utilisant la fonction set . Certains templates contiennent des variables prédéfinies. Le template principal, par exemple (pagelayout.tpl), permet l'accès à tout un ensemble de variables .

Creating and destroying variables / Création et destruction de variable

All variables used in a template must be declared and defined by the "def" function (short for define) before they can be used. A variable exists until the "undef" function (short for undefine) is used in order to destroy it. A previously declared variable will be automatically destroyed at the end of the template file in which it was created. The following example demonstrates the most basic use of the "def" and "undef" functions.
Toutes les variables utilisées dans un template sont déclarées et définies par la fonction def (raccourci de define) avant d'être utilisées. Une variable existe tant que la fonction undef (raccourci de undefine) n'est pas utilisée pour détruire cette variable. Une variable préalablement déclarée sera automatiquement détruite à la fin du template dans lequel elle a été créée. L'exemple suivant illustre l'utilisation la plus simple des fonctions def et undef:

{def $temperature=32}
 
{$temperature}
 
{undef}

The output of the example will be "32". After the {undef} function is called, the $temperature variable will not be available. Both the "def" and the "undef" function can be used with multiple variables at the same time. In addition, the "undef" function can be used without any parameters. When called without parameters the "undef" function automatically destroys all variables that were previously created within the template. The following example demonstrates how the "def" and "undef" functions can be used to create and destroy multiple variables at the same time.
Le code renvoyé par l'exemple ci-dessus sera "32". Une fois la fonction {undef} appelée, la variable $temperature ne sera plus disponible. Les deux fonctions def et undef peuvent être utilisées simultanément avec de nombreuses variables. De plus, la fonction undef peut être utilisée sans aucun paramètre. Dans ce cas, lorsqu'elle est invoquée, cette fonction détruit automatiquement toutes les variables qui ont été préalablement créées dans le template. L'exemple ci-dessous illustre l'utilisation des fonctions def et undef pour la création et la destruction simultané de plusieurs variables:

{def $weather='warm' $celsius=32 $fahrenheit=90}
 
 The weather is {$weather}: {$celsius} C / {$fahrenheit} F <br />
 
 {undef $celsius $fahrenheit}
 
 The weather is still {$weather}. <br />
 
{undef}

The output of this example will be:
La sortie de cet exemple sera:

The weather is warm: 32 C / 90 F
The weather is still warm.

In the example above, the "def" function is used to create three new variables: $temperature, $celsius and $fahrenheit. The "undef" function is used twice. The first time, it is used to destroy the $celsius and $fahrenheit variables. The second is time it is called without parameters and thus the remaining variables (in this case only $temperature) will be destroyed. For more information, please refer to the documentation page of the "def" and "undef" functions.
Dans l'exemple ci-dessus, la fonction def crée trois nouvelles variables: $temperature, $celsius et $fahrenheit. La fonction undef est utilisée deux fois. La première fois, pour détruire les variables $celsius et $fahrenheit. La seconde fois, étant appelée sans paramètre, les variables restantes (dans ce cas, seulement $temperature) seront détruites. Pour plus d'information, référez-vous à la page de documentation des fonctions def et undef .

Changing the contents of variables / Modifier le contenu des variables

The contents/value of a variable can be changed at any time using the "set" function. Please note that this function can be used to change the value of any variable, regardless of if it was created by the system or inside a template. No warning will be given if a system variable is changed. The "set" function can be used to change the value of any variable regardless of the variable's current type and the type of the new value. In other words, this function is capable of changing the type of a variable. The "set" function can not be used to change the value of an element/attribute of an array, hash or an object. In fact, the elements/attributes of arrays, hashes and objects can not be changed from within template code. The following example demonstrates the usage of the "set" function.
Le contenu ou la valeur d'une variable peut être modifié à tout moment avec la fonction set . Notez que cette fonction peut modifier la valeur de n'importe quelle variable, sans se préoccuper de savoir si cette dernière a été créée par le système ou dans le template. Aucun message d'alerte ne sera envoyé si une variable système est modifiée. La fonction set peut modifier la valeur de n'importe quelle variable sans se préoccuper du type courant de la variable ou du type de la nouvelle valeur. En clair, cette fonction peut modifier le type d'une variable. En revanche, elle ne peut modifier la valeur d'un élément ou d'un attribut d'un tableau, d'un hash (tableau associatif) ou encore d'un objet. En fait, les éléments ou attributs des tableaux, des hashes et des objets ne peuvent être modifiés à partir du code d'un template. L'exemple suivant illustre l'utilisation de la fonction set:

{def $weather='warm'}
 
The weather is {$weather}. <br />
 
{set $weather='cold'}
 
The weather is {$weather}.
 
{undef}

The output of the example will be:
La sortie produite par cet exemple sera:

The weather is warm.
The weather is cold.

Just like the "def" and "undef" functions, the "set" function can work with multiple variables at the same time. For more information, please refer to the documentation page of the "set" function.
A l'instar des fonctions def et undef, la fonction set peut définir plusieurs variables en même temps. Pour plus d'informations, référez-vous à la page de documentation de la fonction set .

Accessing array elements / Accéder aux éléments d'un tableau

The elements of a simple/vector array can only be accessed using numerical indexes. This method is called "index lookup". The elements of an associative array can be accessed by using the key identifiers. This method is called "identifier lookup". The following example demonstrates the different lookup methods.
On ne peut accéder aux éléments d'un tableau simple (ou vector - dont les éléments sont de type String) que par le biais de l'utilisation des index numériques. Cette méthode est appelée «index lookup» (consultation par index). On peut accéder aux éléments d'un tableau associatif en utilisant les identifiants des clefs. Cette méthode est appelée «identifier lookup» (consultation par identifiants). Les exemples qui suivent illustrent les différentes méthodes de consultation des tableaux.

Index look-up / Consultation par index

Index look-up is carried out by appending a period/dot and an index number to the name of a simple/vector or associative array. Index look-up may also be carried out by appending a matching pair of brackets that encapsulate the desired index value. The following example demonstrates how to access the elements of a simple array using index look-up. Please note the different syntax (dot and brackets).
La consultation par index est réalisée en ajoutant un point et un numéro d'index au nom d'un tableau simple (vector) ou associatif. La consultation par index peut être également effectuée en ajoutant une paire de crochets de correspondance contenant la valeur de l'index désirée. L'exemple suivant montre comment accéder aux éléments d'un tableau simple en utilisant la consultation par index. Notez les différentes syntaxes (point et crochets):

{def $sentence=array( 'Life', 'is', 'very', 'good!' )}
 
The 1st element is: {$sentence.0} <br />
The 2nd element is: {$sentence.1} <br />
The 3rd element is: {$sentence[2]} <br />
The 4th element is: {$sentence[3]} <br />
 
{undef}

The code above will output the following:
La sortie produite par le code ci-dessus sera:

The 1st element is: Life
The 2nd element is: is
The 3rd element is: very
The 4th element is: good!

Identifier look-up / Consultation par identifiants

Identifier look-up can be carried out by appending a period/dot and an identifier name to the name of an associative array. Identifier look-up may also be carried out by appending a matching pair of brackets that encapsulate the desired index value. The following example demonstrates how to access the elements of an associative array using the identifier look-up method. Notice the different syntax (use of dot and brackets).
La consultation par identifiant est réalisée en ajoutant un point et un nom d'identifiant au nom d'un tableau associatif. La consultation par identifiant peut être également effectuée en ajoutant une paire de crochets de correspondance contenant la valeur de l'index désirée. L'exemple ci-dessous montre comment accéder aux éléments d'un tableau associatif en utilisant la consultation par identifiant. Notez les différentes syntaxes (point et crochets):

{def $sentence=hash( 'first', ;'Life',
'second', 'is',
'third', &nbsp;'very',
'fourth', 'good!' )}
 
The 1st element is: {$sentence.first} <br />
The 2nd element is: {$sentence.second} <br />
The 3rd element is: {$sentence[third]} <br />
The 4th element is: {$sentence[fourth]} <br />
 
{undef}

The following output will be produced:
La sortie suivante sera produite:

The 1st element is: Life
The 2nd element is: is
The 3rd element is: very
The 4th element is: good!

Accessing object attributes / Accéder aux attributs d'un objet

The attributes of an object can only be accessed using the attributes' identifiers. An identifier is just the name of an attribute (similar to the keys of an associative array). The following example demonstrates how the different attributes of a node object can be accessed from within a template.
On ne peut accéder aux attributs d'un objet qu'au moyen des identifiants des attributs. Un identifiant représente simplement le nom d'un attribut (ce qui est similaire aux clefs des tableau associatifs). L'exemple suivant montre comment accéder aux différents attributs d'un objet à partir d'un template:

The ID of the node: {$node.node_id} <br />
 
The ID of the object encapsulated by the node: {$node.object.id} <br />
 
The name of the object: {$node.object.name} <br />
 
First time the object was published: {$node.object.published|l10n( shortdate )} <br /> 

If the $node variable contains a node that has ID number 44 and encapsulates object number 13 named "Birthday" published on the first of April in 2003, the following output will be produced:
Si la variable $node contient un nœud de numéro de ID "44" et encapsulant l'objet numéro 13 appelé Birthday et publié le 1er Avril 2003, alors la sortie suivante sera produite:

The ID of the node: 44
The ID of the object encapsulated by the node: 13
The name of the object: Birthday
First time the object was published: 01/04/2003


Table des matières

  1. The template language -- Langage des templates
  2. Comments -- Commentaires
  3. Variable types -- Types de variables
  4. Variable usage -- Utilisation des variables
  5. Array and object inspection -- Inspection des tableaux et objets
  6. Control structures -- Structures de contrôle
  7. Functions and operators -- Fonctions et opérateurs

Commentaires