Files
smarty/docs/fr/designers/language-builtin-functions/language-function-section.xml
2006-10-14 14:54:52 +00:00

838 lines
23 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<!-- EN-Revision: 1.19 Maintainer: yannick Status: ready -->
<sect1 id="language.function.section">
<title>{section},{sectionelse}</title>
<para>
Une <varname>{section}</varname>
sert à boucler dans des <emphasis role="bold">tableaux de données</emphasis>,
contrairement à <link linkend="language.function.foreach"><varname>{foreach}</varname></link>
qui est utilisé pour boucler dans un
<emphasis role="bold">simple tableau associatif</emphasis>.
Chaque balise <varname>{section}</varname> doit aller de paire avec une
balise <varname>{/section}</varname> fermante.
</para>
<informaltable frame="all">
<tgroup cols="5">
<colspec colname="param" align="center" />
<colspec colname="type" align="center" />
<colspec colname="required" align="center" />
<colspec colname="default" align="center" />
<colspec colname="desc" />
<thead>
<row>
<entry>Nom attribut</entry>
<entry>Type</entry>
<entry>Requis</entry>
<entry>Défaut</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>name</entry>
<entry>chaîne de caractère</entry>
<entry>Oui</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>Le nom de la section</entry>
</row>
<row>
<entry>loop</entry>
<entry>mixed</entry>
<entry>Oui</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>Valeur qui détermine le nombre de fois que la boucle sera exécutée</entry>
</row>
<row>
<entry>start</entry>
<entry>entier</entry>
<entry>Non</entry>
<entry><emphasis>0</emphasis></entry>
<entry>
La position de l'index ou la section commencera son
parcours. Si la valeur donnée est négative, la position de
départ est calculée depuis la fin du tableau. Par exemple,
s'il existe 7 valeurs dans le tableau à parcourir et que start
est à -2, l'index de départ sera 5. Les valeurs incorrectes
(en dehors de la portée du tableau) sont automatiquements
tronquées à la valeur correcte la plus proche
</entry>
</row>
<row>
<entry>step</entry>
<entry>entier</entry>
<entry>Non</entry>
<entry><emphasis>1</emphasis></entry>
<entry>La valeur du pas qui sera utilisé pour parcourir le
tableau.Par exemple, step=2 parcourera les indices
0,2,4, etc. Si step est négatif, le tableau sera parcouru en sens
inverse</entry>
</row>
<row>
<entry>max</entry>
<entry>entier</entry>
<entry>Non</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>Définit le nombre maximum de fois que le tableau sera
parcouru</entry>
</row>
<row>
<entry>show</entry>
<entry>booléen</entry>
<entry>No</entry>
<entry><emphasis>&true;</emphasis></entry>
<entry>Détermine s'il est nécessaire d'afficher la
section ou non</entry>
</row>
</tbody>
</tgroup>
</informaltable>
<itemizedlist>
<listitem><para>
Les paramètres requis sont <parameter>name</parameter> et <parameter>loop</parameter>.
</para></listitem>
<listitem><para>
Le <parameter>name</parameter> de la <varname>{section}</varname> est, selon votre choix,
composé de lettres, chiffres et underscores, comme pour les
<ulink url="&url.php-manual;language.variables">variables PHP</ulink>.
</para></listitem>
<listitem><para>
Les sections peuvent être imbriquées mais leurs noms doivent être uniques.
</para></listitem>
<listitem><para>
L'attribut <parameter>loop</parameter>, habituellement un tableau de valeurs,
détermine le nombre de fois que
<varname>{section}</varname> doit boucler.
</para></listitem>
<listitem><para>
Lors de l'affichage d'une variable dans une <varname>{section}</varname>, le nom de la
<varname>{section}</varname> doit être fournis après le nom de la variable entre crochets [].
</para></listitem>
<listitem><para>
<varname>{sectionelse}</varname> est exécuté lorsqu'aucune valeur n'est trouvée dans la variable à
parcourir.
</para></listitem>
<listitem><para>
<varname>{section}</varname> a également ces propres variables qui gérent les propriétés
de la <varname>{section}</varname>.
Ces propriétés sont accessibles comme ceci : <link linkend="language.variables.smarty.loops">
<parameter>{$smarty.section.name.property}</parameter></link>
<quote>name</quote> est l'attribut <parameter>name</parameter>.
</para></listitem>
<listitem><para>
Les propriétés de <varname>{section}</varname> sont
<link linkend="section.property.index"><parameter>index</parameter></link>,
<link linkend="section.property.index.prev"><parameter>index_prev</parameter></link>,
<link linkend="section.property.index.next"><parameter>index_next</parameter></link>,
<link linkend="section.property.iteration"><parameter>iteration</parameter></link>,
<link linkend="section.property.first"><parameter>first</parameter></link>,
<link linkend="section.property.last"><parameter>last</parameter></link>,
<link linkend="section.property.rownum"><parameter>rownum</parameter></link>,
<link linkend="section.property.loop"><parameter>loop</parameter></link>,
<link linkend="section.property.show"><parameter>show</parameter></link>,
<link linkend="section.property.total"><parameter>total</parameter></link>.
</para></listitem>
</itemizedlist>
<example>
<title>Boucler dans un simple tableau avec {section}</title>
<para>
<link linkend="api.assign"><varname>assign()</varname></link> un tableau à Smarty
</para>
<programlisting role="php">
<![CDATA[
<?php
$data = array(1000,1001,1002);
$smarty->assign('custid',$data);
?>
]]>
</programlisting>
<para>Le template qui affiche le tableau</para>
<programlisting>
<![CDATA[
{* Cet exemple affichera toutes les valeurs du tableau $custid *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br />
{/section}
<hr />
{* Affiche toutes les valeurs du tableau $custid, en ordre inverse *}
{section name=foo loop=$custid step=-1}
{$custid[foo]}<br />
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
id: 1000<br />
id: 1001<br />
id: 1002<br />
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br />
]]>
</screen>
</example>
<example>
<title>{section} sans un tableau assigné</title>
<programlisting>
<![CDATA[
{section name=foo start=10 loop=20 step=2}
{$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
{$smarty.section.bar.index}
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
10 12 14 16 18
<hr />
20 18 16 14 12 10
]]>
</screen>
</example>
<example>
<title>Nommage d'une {section}</title>
<para>
Le <parameter>name</parameter> de la <varname>{section}</varname> peut être ce que vous
voulez, voir les <ulink url="&url.php-manual;language.variables">variables PHP</ulink>.
Il sera utilisé pour référencer les données de la <varname>{section}</varname>.
</para>
<programlisting>
<![CDATA[
{section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]}
{$address[anything].bar}
{/section}
]]>
</programlisting>
</example>
<example>
<title>Boucler dans un tableau associatif avec {section}</title>
<para>
Voici un exemple d'affichage d'un tableau associatif de données avec
<varname>{section}</varname>. Ce qui suit est le script PHP assignant
le tableau <parameter>$contacts</parameter> à Smarty.
</para>
<programlisting role="php">
<![CDATA[
<?php
$data = array(
array('name' => 'John Smith', 'home' => '555-555-5555',
'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
array('name' => 'Jack Jones', 'home' => '777-555-5555',
'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
array('name' => 'Jane Munson', 'home' => '000-555-5555',
'cell' => '123456', 'email' => 'jane@myexample.com')
);
$smarty->assign('contacts',$data);
?>
]]>
</programlisting>
<para>Le template pour afficher <parameter>$contacts</parameter></para>
<programlisting>
<![CDATA[
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}<br />
home: {$contacts[customer].home}<br />
cell: {$contacts[customer].cell}<br />
e-mail: {$contacts[customer].email}
</p>
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
<p>
name: John Smith<br />
home: 555-555-5555<br />
cell: 666-555-5555<br />
e-mail: john@myexample.com
</p>
<p>
name: Jack Jones<br />
home phone: 777-555-5555<br />
cell phone: 888-555-5555<br />
e-mail: jack@myexample.com
</p>
<p>
name: Jane Munson<br />
home phone: 000-555-5555<br />
cell phone: 123456<br />
e-mail: jane@myexample.com
</p>
]]>
</screen>
</example>
<example>
<title>{section} démontrant l'utilisation de la variable <varname>loop</varname></title>
<para>Cet exemple suppose que <parameter>$custid</parameter>, <parameter>$name</parameter>
et <parameter>$address</parameter> sont tous des tableaux contenant le même
nombre de valeurs. Tout d'abord, le script PHP qui assigne les tableaux à Smarty.</para>
<programlisting role="php">
<![CDATA[
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
$addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);
?>
]]>
</programlisting>
<para>La variable <parameter>loop</parameter> détermine uniquement le nombre
de fois qu'il faut boucler.
Vous pouvez accéder à n'importe quelle variable du template dans la
<varname>{section}</varname></para>
<programlisting>
<![CDATA[
{section name=customer loop=$custid}
<p>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}
</p>
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
<p>
id: 1000<br />
name: John Smith<br />
address: 253 Abbey road
</p>
<p>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln
</p>
<p>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st
</p>
]]>
</screen>
</example>
<example>
<title>{section} imbriquée</title>
<para>
Les sections peuvent être imbriquées autant de fois que vous le voulez.
Avec les sections imbriquées, vous pouvez accéder aux structures de données
complexes, comme les tableaux multi-dimentionnels. Voici un script PHP qui assigne les
tableaux.
</para>
<programlisting role="php">
<![CDATA[
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);
$types = array(
array( 'home phone', 'cell phone', 'e-mail'),
array( 'home phone', 'web'),
array( 'cell phone')
);
$smarty->assign('contact_type', $types);
$info = array(
array('555-555-5555', '666-555-5555', 'john@myexample.com'),
array( '123-456-4', 'www.example.com'),
array( '0457878')
);
$smarty->assign('contact_info', $info);
?>
]]>
</programlisting>
<para>Dans ce template, <emphasis>$contact_type[customer]</emphasis> est un tableau de
types de contacts.</para>
<programlisting>
<![CDATA[
{section name=customer loop=$custid}
<hr>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}<br />
{section name=contact loop=$contact_type[customer]}
{$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
{/section}
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
<hr>
id: 1000<br />
name: John Smith<br />
address: 253 N 45th<br />
home phone: 555-555-5555<br />
cell phone: 666-555-5555<br />
e-mail: john@myexample.com<br />
<hr>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln<br />
home phone: 123-456-4<br />
web: www.example.com<br />
<hr>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st<br />
cell phone: 0457878<br />
]]>
</screen>
</example>
<example>
<title>Exemple avec une base de données et {sectionelse}</title>
<para>Les résultats d'une recherche dans une base de données
(e.g. ADODB ou PEAR) sont assignés à Smarty</para>
<programlisting role="php">
<![CDATA[
<?php
$sql = 'select id, name, home, cell, email from contacts '
."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>
]]>
</programlisting>
<para>Le template pour afficher le résultat de la base de données dans un tableau HTML</para>
<programlisting>
<![CDATA[
<table>
<tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
{section name=co loop=$contacts}
<tr>
<td><a href="view.php?id={$contacts[co].id}">view<a></td>
<td>{$contacts[co].name}</td>
<td>{$contacts[co].home}</td>
<td>{$contacts[co].cell}</td>
<td>{$contacts[co].email}</td>
<tr>
{sectionelse}
<tr><td colspan="5">Aucun élément n'a été trouvé</td></tr>
{/section}
</table>
]]>
</programlisting>
</example>
<sect2 id="section.property.index">
<title>.index</title>
<para>
<parameter>index</parameter> contient l'index courant du tableau, en commençant par zéro ou par
<parameter>start</parameter> s'il est fourni. Il s'incrémente d'un en un ou de
<parameter>step</parameter> s'il est fourni.
</para>
<note>
<title>Note technique</title>
<para>
Si les propriétés <parameter>step</parameter> et <parameter>start</parameter>
ne sont pas modifiés, alors le fonctionnement est le même que celui de la propriété
<link linkend="section.property.iteration"><parameter>iteration</parameter></link>,
mise à part qu'il commence à zéro au lieu de un.
</para>
</note>
<example>
<title>Exemple avec la propriété <varname>index</varname></title>
<para>
<note><title>FYI</title>
<para><literal>$custid[customer.index]</literal> et
<literal>$custid[customer]</literal> sont identiques.</para>
</note>
</para>
<programlisting>
<![CDATA[
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
]]>
</screen>
</example>
</sect2>
<sect2 id="section.property.index.prev">
<title>.index_prev</title>
<para>
<parameter>index_prev</parameter> est l'index de la boucle précédente.
Lors de la première boucle, il vaut -1.
</para>
</sect2>
<sect2 id="section.property.index.next">
<title>.index_next</title>
<para>
<parameter>index_next</parameter> est l'index de la prochaine boucle.
Lors de la prochaine boucle, il vaudra un de moins que l'index courant, suivant
la configuration de l'attribut <parameter>step</parameter>, s'il est fourni.
</para>
<example>
<title>Exemple avec les propriétés <varname>index</varname>, <varname>index_next</varname>
et <varname>index_prev</varname></title>
<programlisting role="php">
<![CDATA[
<?php
$data = array(1001,1002,1003,1004,1005);
$smarty->assign('rows',$data);
?>
]]>
</programlisting>
<para>Le template pour afficher le tableau ci-dessus dans un tableau HTML</para>
<programlisting>
<![CDATA[
{* $rows[row.index] et $rows[row] sont identiques *}
<table>
<tr>
<th>index</th><th>id</th>
<th>index_prev</th><th>prev_id</th>
<th>index_next</th><th>next_id</th>
</tr>
{section name=row loop=$rows}
<tr>
<td>{$smarty.section.row.index}</td><td>{$rows[row]}</td>
<td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td>
<td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td>
</tr>
{/section}
</table>
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera un tableau HTML contenant :
</para>
<screen>
<![CDATA[
index id index_prev prev_id index_next next_id
0 1001 -1 1 1002
1 1002 0 1001 2 1003
2 1003 1 1002 3 1004
3 1004 2 1003 4 1005
4 1005 3 1004 5
]]>
</screen>
</example>
</sect2>
<sect2 id="section.property.iteration">
<title>.iteration</title>
<para>
<parameter>iteration</parameter> contient l'itération courante de la boucle et commence à un.
</para>
<note>
<para>
Ceci n'est pas affecté par les propriétés <varname>{section}</varname>
<parameter>start</parameter>, <parameter>step</parameter> et
<parameter>max</parameter> contrairement à la propriété
<link linkend="section.property.index"><parameter>index</parameter></link>.
<parameter>iteration</parameter> commence également à un au lieu de zéro
contrairement à <parameter>index</parameter>.
<link linkend="section.property.rownum"><parameter>rownum</parameter></link>
est un alias de <parameter>iteration</parameter>, ils sont identiques.
</para>
</note>
<example>
<title>Exemple avec la propriété <varname>iteration</varname></title>
<programlisting role="php">
<![CDATA[
<?php
// array of 3000 to 3015
$id = range(3000,3015);
$smarty->assign('arr',$id);
?>
]]>
</programlisting>
<para>Le template pour afficher tous les autres éléments du tableau <literal>$arr</literal> comme
<literal>step=2</literal></para>
<programlisting>
<![CDATA[
{section name=cu loop=$arr start=5 step=2}
iteration={$smarty.section.cu.iteration}
index={$smarty.section.cu.index}
id={$custid[cu]}<br />
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
iteration=1 index=5 id=3005<br />
iteration=2 index=7 id=3007<br />
iteration=3 index=9 id=3009<br />
iteration=4 index=11 id=3011<br />
iteration=5 index=13 id=3013<br />
iteration=6 index=15 id=3015<br />
]]>
</screen>
<para>
Un autre exemple d'utilisation de la propriété
<parameter>iteration</parameter> est d'afficher un bloc d'en-tête d'un tableau toutes
les 5 lignes.
Utilisez la fonction <link linkend="language.function.if"><varname>{if}</varname></link>
avec l'opérateur mod.
</para>
<programlisting>
<![CDATA[
<table>
{section name=co loop=$contacts}
{if $smarty.section.co.iteration % 5 == 1}
<tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
{/if}
<tr>
<td><a href="view.php?id={$contacts[co].id}">view<a></td>
<td>{$contacts[co].name}</td>
<td>{$contacts[co].home}</td>
<td>{$contacts[co].cell}</td>
<td>{$contacts[co].email}</td>
<tr>
{/section}
</table>
]]>
</programlisting>
</example>
</sect2>
<sect2 id="section.property.first">
<title>.first</title>
<para>
<parameter>first</parameter> est défini à &true; si l'itération courante de
<varname>{section}</varname> est l'initiale.
</para>
</sect2>
<sect2 id="section.property.last">
<title>.last</title>
<para>
<parameter>last</parameter> est défini à &true;
si l'itération courante de la section est la dernière.
</para>
<example>
<title>Exemple avec les propriétés <varname>first</varname> et <varname>last</varname></title>
<para>
Cet exemple boucle sur le tableau <varname>$customers</varname>,
affiche un bloc d'en-tête lors de la première itération et, lors de la dernière,
affiche un bloc de pied de page. Utilise aussi la propriété
<link linkend="section.property.total"><parameter>total</parameter></link>.
</para>
<programlisting>
<![CDATA[
{section name=customer loop=$customers}
{if $smarty.section.customer.first}
<table>
<tr><th>id</th><th>customer</th></tr>
{/if}
<tr>
<td>{$customers[customer].id}}</td>
<td>{$customers[customer].name}</td>
</tr>
{if $smarty.section.customer.last}
<tr><td></td><td>{$smarty.section.customer.total} customers</td></tr>
</table>
{/if}
{/section}
]]>
</programlisting>
</example>
</sect2>
<sect2 id="section.property.rownum">
<title>.rownum</title>
<para>
<parameter>rownum</parameter> contient l'itération courante de la boucle,
commençant à un. C'est un alias de <link
linkend="section.property.iteration"><parameter>iteration</parameter></link>,
ils fonctionnent exactement de la même façon.
</para>
</sect2>
<sect2 id="section.property.loop">
<title>.loop</title>
<para>
<parameter>loop</parameter> contient le dernier index de la boucle de la section.
Il peut être utilisé dans ou après la <varname>{section}</varname>.
</para>
<example>
<title>Exemple avec la propriété <varname>loop</varname></title>
<programlisting>
<![CDATA[
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There are {$smarty.section.customer.loop} customers shown above.
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
There are 3 customers shown above.
]]>
</screen>
</example>
</sect2>
<sect2 id="section.property.show">
<title>.show</title>
<para>
<parameter>show</parameter> est utilisé en tant que paramètre à la section et est une valeur booléenne.
S'il vaut &false;, la section ne sera pas affichée. S'il y a un
<varname>{sectionelse}</varname>, il sera affiché de façon alternative.
</para>
<example>
<title>Exemple avec la propriété <varname>show</varname></title>
<para>Une valeur booléenne <varname>$show_customer_info</varname> est passée
depuis l'application PHP, pour réguler l'affichage ou non de cette section.</para>
<programlisting>
<![CDATA[
{section name=customer loop=$customers show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$customers[customer]}<br />
{/section}
{if $smarty.section.customer.show}
the section was shown.
{else}
the section was not shown.
{/if}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />
the section was shown.
]]>
</screen>
</example>
</sect2>
<sect2 id="section.property.total">
<title>.total</title>
<para>
<parameter>total</parameter> contient le nombre d'itérations que cette
<varname>{section}</varname> bouclera. Il peut être utilisé dans ou après une
<varname>{section}</varname>.
</para>
<example>
<title>Exemple avec la propriété <varname>total</varname></title>
<programlisting>
<![CDATA[
{section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There are {$smarty.section.customer.total} customers shown above.
]]>
</programlisting>
</example>
<para>
Voir aussi
<link linkend="language.function.foreach"><varname>{foreach}</varname></link> et
<link linkend="language.variables.smarty.loops"><parameter>$smarty.section</parameter></link>.
</para>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->