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

Control structures -- Structures de contrôle

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

28/09/2010 01:27

versions 3.9, 3.10, 4.x

The eZ Publish template language offers a selection of mechanisms that can be used to solve common programmatic issues like for example condition control, looping, etc. The following list shows an overview of the available mechanisms:
Le langage de template de eZ Publish offre un ensemble de mécanisme utilisés pour résoudre les questions de programmation les plus courantes, comme par exemple le contrôle de conditions (???), les boucles, etc... Voici la liste des mécanismes disponibles:

  • IF-THEN-ELSE
  • SWITCH
  • WHILE
  • DO...WHILE
  • FOR
  • FOREACH

IF-THEN-ELSE

The IF construct allows for conditional execution of code fragments. It is one of the most important features of many programming languages. The eZ Publish implementation makes it possible to do conditional branching by the way of the following elements: IF, ELSE and ELSEIF. The ELSE and ELSEIF elements are optional. The following examples demonstrate the use of this construct.
La contruction IF permet l'exécution conditionnelle de morceaux de code. C'est l'une des plus importantes fonctions de bon nombres de langages de programmation. Son implémentation dans eZ Publish permet de faire des branchements conditionnels par le biais des éléments suivants: IF, ELSE et ELSEIF. Les éléments ELSE et ELSEIF sont optionnels. Les exemples suivants illustrent l'utilisation de cette construction:

Exemple 1

{if eq( $var, 128 )}
Hello world <br />
 {else}
No world here, move along. <br />
 {/if}

Exemple 2

{if eq( $fruit, 'apples' )}
Apples <br />
 {elseif eq( $fruit, 'oranges' )}
Oranges <br />
 {else}
Bananas <br />
 {/if}

SWITCH

The SWITCH mechanism is similar to a series of IF statements used on the same expression. This construct is typically useful when the same variable needs to be compared to different values. It executes a piece of code depending on which value that matched a given criteria. The following example demonstrates basic use of this construct.
Le mécanisme SWITCH se comporte comme une série de déclarations IF utilisées dans la même expression. Cette construction est particulièrement utile lorsqu'une même variable doit être comparée à différentes valeurs. Elle exécute le bout de code lié à la valeur satisfaisant le critère de correspondance. L'exemple suivant illustre une utilisation simple de cette construction:

{switch match=$fruits}
 
{case match='apples'}
Apples <br />
{/case}
 
{case match='oranges'}
Oranges <br />
{/case}
 
{case}
Unidentified fruit! <br />
{/case}
 
{/switch}

If the value of the $fruits variable is "oranges", the following output will be produced:
Si la valeur de la variable $fruit est oranges, la sortie suivante sera produite:

Oranges

WHILE

The WHILE construct is the simplest loop mechanism that the template language offers. It tells eZ Publish to execute the nested statement(s) repeatedly, as long as a given expression evaluates to TRUE. The value of the expression is checked for every loop iteration (at the beginning of the iteration). If the given expression evaluates to FALSE from the very beginning, the nested statement(s) will not be executed. The following example demonstrates basic use of this construct.
La construction WHILE représente le plus simple mécanisme de boucle qu'offre le langage de template. Cette construction indique à eZ Publish d'exécuter plusieurs fois les déclarations imbriquées et ce, jusqu'à ce qu'une expression donnée soit VRAIE. La valeur de cette expression est vérifiée à chaque itération de la boucle (au départ de l'itération et non à la fin). Si l'expression donnée est FAUSSE dès le début, les déclarations imbriquées ne seront pas exécutées. L'exemple suivant illustre une utilisation simple de cette construction:

{while ne( $counter, 8 )}
 
Print this line eight times ({$counter}) <br />
{set $counter=inc( $counter )}
 
{/while}

If the initial value of $counter is zero, the following output will be produced:
Si la valeur initiale de la variable $counter est zéro, la sortie suivante sera produite:

Print this line eight times (0)
Print this line eight times (1)
Print this line eight times (2)
Print this line eight times (3)
Print this line eight times (4)
Print this line eight times (5)
Print this line eight times (6)
Print this line eight times (7)

DO...WHILE

A DO...WHILE loop is very similar to WHILE loops, except that the expression is checked at the end of each iteration instead of in the beginning. The main difference is that this construct will always execute the first iteration (regardless of how the test expression evaluates). The following example demonstrates basic use of this construct.
Une boucle DO...WHILE est très similaire aux boucles WHILE, excepté que l'expression est vérifiées à la fin de chaque itération au lieu de l'être au début. La principale différence est que cette construction exécute toujours la première itération (sans se soucier de la façon dont l'expression de test évalue). L'exemple suivant illustre une utilisation simple de cette construction:

{do}
 
Keep printing this line ({$counter}) <br />
{set $counter=inc( $counter )}
;
{/do while ne( $counter, 8 )}

If the initial value of $counter is 0, the following output will be produced:
Si la valeur initiale de la variable $counter est 0, la sortie suivante sera produite:

Keep printing this line (0)
Keep printing this line (1)
Keep printing this line (2)
Keep printing this line (3)
Keep printing this line (4)
Keep printing this line (5)
Keep printing this line (6)
Keep printing this line (7)
Keep printing this line (8)

FOR

Generic looping may be achieved using FOR loops. This construct supports looping over numerical ranges in both directions. In addition it also supports breaking, continual and skipping. The following example demonstrates basic use of this construct.
Les boucles génériques peuvent être réalisées en utilisant les boucles FOR . Cette construction supporte les boucles numériques dans les deux sens (croissantes ou décroissantes). Cette structure permet par ailleurs d'arrêter une boucle, de la laisser se dérouler normalement ou bien de sauter/passer la boucle. L'exemple suivant illustre une utilisation simple de cette construction:

{for 0 to 7 as $counter}
 
Value of counter: {$counter} <br />
 
 {/for}

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

Value of counter: 0
Value of counter: 1
Value of counter: 2
Value of counter: 3
Value of counter: 4
Value of counter: 5
Value of counter: 6
Value of counter: 7

FOREACH

The FOREACH construct can be used to iterate over arrays in different ways. The loop can be tweaked using miscellaneous techniques. The following example demonstrates basic use of this construct.
La construction FOREACH est utilisée pour parcourir un tableau de différente manières. La boucle peut prendre différentes formes en utilisant diverses techniques. L'exemple suivant illustre une utilisation simple de cette construction:

{foreach $objects as $object}
 
{$object.name} <br />
 
{/foreach}

The example above will print out the names of the objects that are stored in the $objects array. If this array stores 4 objects with the following names: "Emmett Brown", "Marty McFly", "Lorraine Baines" and "Biff Tannen", the following output will be produced:
L'exemple ci-dessus affichera les noms des objets stockés dans le tableau $objects. Si ce tableau stocke 4 objets nommés Emmett Brown, Marty McFly, Lorraine Baineset Biff Tannen, la sortie suivante sera alors produite:

Emmett Brown
Marty McFly
Lorraine Baines
Biff Tannen


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