Files
smarty/docs/fr/designers/language-builtin-functions/language-function-section.xml

838 lines
23 KiB
XML
Raw Normal View History

2004-04-13 15:43:47 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
2006-10-14 14:54:52 +00:00
<!-- EN-Revision: 1.19 Maintainer: yannick Status: ready -->
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<sect1 id="language.function.section">
2005-12-10 22:09:38 +00:00
<title>{section},{sectionelse}</title>
2006-10-09 15:55:58 +00:00
<para>
Une <varname>{section}</varname>
sert <20> boucler dans des <emphasis role="bold">tableaux de donn<6E>es</emphasis>,
contrairement <20> <link linkend="language.function.foreach"><varname>{foreach}</varname></link>
qui est utilis<69> 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>
2004-05-23 15:44:59 +00:00
<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>
2006-10-09 15:55:58 +00:00
<entry>D<EFBFBD>faut</entry>
2004-05-23 15:44:59 +00:00
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>name</entry>
<entry>cha<EFBFBD>ne de caract<63>re</entry>
<entry>Oui</entry>
<entry><emphasis>n/a</emphasis></entry>
<entry>Le nom de la section</entry>
</row>
<row>
<entry>loop</entry>
2004-12-21 21:08:13 +00:00
<entry>mixed</entry>
2004-05-23 15:44:59 +00:00
<entry>Oui</entry>
<entry><emphasis>n/a</emphasis></entry>
2006-10-09 15:55:58 +00:00
<entry>Valeur qui d<>termine le nombre de fois que la boucle sera ex<65>cut<75>e</entry>
2004-05-23 15:44:59 +00:00
</row>
<row>
<entry>start</entry>
<entry>entier</entry>
<entry>Non</entry>
<entry><emphasis>0</emphasis></entry>
2006-10-09 15:55:58 +00:00
<entry>
La position de l'index ou la section commencera son
2004-05-23 15:44:59 +00:00
parcours. Si la valeur donn<6E>e est n<>gative, la position de
d<>part est calcul<75>e depuis la fin du tableau. Par exemple,
s'il existe 7 valeurs dans le tableau <20> parcourir et que start
est <20> -2, l'index de d<>part sera 5. Les valeurs incorrectes
(en dehors de la port<72>e du tableau) sont automatiquements
2006-10-09 15:55:58 +00:00
tronqu<71>es <20> la valeur correcte la plus proche
</entry>
2004-05-23 15:44:59 +00:00
</row>
<row>
<entry>step</entry>
<entry>entier</entry>
<entry>Non</entry>
<entry><emphasis>1</emphasis></entry>
<entry>La valeur du pas qui sera utilis<69> 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
2006-10-09 15:55:58 +00:00
inverse</entry>
2004-05-23 15:44:59 +00:00
</row>
<row>
<entry>max</entry>
<entry>entier</entry>
<entry>Non</entry>
2006-10-09 15:55:58 +00:00
<entry><emphasis>n/a</emphasis></entry>
2004-05-23 15:44:59 +00:00
<entry>D<EFBFBD>finit le nombre maximum de fois que le tableau sera
2006-10-09 15:55:58 +00:00
parcouru</entry>
2004-05-23 15:44:59 +00:00
</row>
<row>
<entry>show</entry>
<entry>bool<EFBFBD>en</entry>
2006-10-09 15:55:58 +00:00
<entry>No</entry>
<entry><emphasis>&true;</emphasis></entry>
2004-05-23 15:44:59 +00:00
<entry>D<EFBFBD>termine s'il est n<>cessaire d'afficher la
2006-10-09 15:55:58 +00:00
section ou non</entry>
2004-05-23 15:44:59 +00:00
</row>
</tbody>
</tgroup>
</informaltable>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
<itemizedlist>
<listitem><para>
Les param<61>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<6F> 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 <20>tre imbriqu<71>es mais leurs noms doivent <20>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 <20>tre fournis apr<70>s le nom de la variable entre crochets [].
</para></listitem>
<listitem><para>
<varname>{sectionelse}</varname> est ex<65>cut<75> lorsqu'aucune valeur n'est trouv<75>e dans la variable <20>
parcourir.
</para></listitem>
<listitem><para>
<varname>{section}</varname> a <20>galement ces propres variables qui g<>rent les propri<72>t<EFBFBD>s
de la <varname>{section}</varname>.
Ces propri<72>t<EFBFBD>s sont accessibles comme ceci : <link linkend="language.variables.smarty.loops">
<parameter>{$smarty.section.name.property}</parameter></link>
o<> <quote>name</quote> est l'attribut <parameter>name</parameter>.
</para></listitem>
<listitem><para>
Les propri<72>t<EFBFBD>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>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
<example>
<title>Boucler dans un simple tableau avec {section}</title>
<para>
<link linkend="api.assign"><varname>assign()</varname></link> un tableau <20> Smarty
</para>
<programlisting role="php">
2005-12-10 22:09:38 +00:00
<![CDATA[
<?php
$data = array(1000,1001,1002);
$smarty->assign('custid',$data);
?>
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>Le template qui affiche le tableau</para>
2005-05-10 21:36:53 +00:00
<programlisting>
2006-10-09 15:55:58 +00:00
<![CDATA[
{* Cet exemple affichera toutes les valeurs du tableau $custid *}
2005-12-10 22:09:38 +00:00
{section name=customer loop=$custid}
id: {$custid[customer]}<br />
{/section}
<hr />
2006-10-09 15:55:58 +00:00
{* Affiche toutes les valeurs du tableau $custid, en ordre inverse *}
2005-12-10 22:09:38 +00:00
{section name=foo loop=$custid step=-1}
{$custid[foo]}<br />
2004-04-13 15:43:47 +00:00
{/section}
2005-05-10 21:36:53 +00:00
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
id: 1000<br />
id: 1001<br />
id: 1002<br />
2005-12-10 22:09:38 +00:00
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br />
]]>
</screen>
2006-10-09 15:55:58 +00:00
</example>
<example>
<title>{section} sans un tableau assign<67></title>
<programlisting>
2005-12-10 22:09:38 +00:00
<![CDATA[
{section name=foo start=10 loop=20 step=2}
2006-10-09 15:55:58 +00:00
{$smarty.section.foo.index}
2005-12-10 22:09:38 +00:00
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
2006-10-09 15:55:58 +00:00
{$smarty.section.bar.index}
2005-12-10 22:09:38 +00:00
{/section}
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
2005-12-10 22:09:38 +00:00
10 12 14 16 18
<hr />
20 18 16 14 12 10
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
2005-05-10 21:36:53 +00:00
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
<example>
<title>Nommage d'une {section}</title>
<para>
Le <parameter>name</parameter> de la <varname>{section}</varname> peut <20>tre ce que vous
voulez, voir les <ulink url="&url.php-manual;language.variables">variables PHP</ulink>.
Il sera utilis<69> pour r<>f<EFBFBD>rencer les donn<6E>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>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
<example>
<title>Boucler dans un tableau associatif avec {section}</title>
<para>
Voici un exemple d'affichage d'un tableau associatif de donn<6E>es avec
<varname>{section}</varname>. Ce qui suit est le script PHP assignant
le tableau <parameter>$contacts</parameter> <20> 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);
2005-12-10 22:09:38 +00:00
?>
]]>
</programlisting>
2006-10-09 15:55:58 +00:00
<para>Le template pour afficher <parameter>$contacts</parameter></para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
{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>
2004-04-13 15:43:47 +00:00
{/section}
2005-05-10 21:36:53 +00:00
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
<p>
2006-10-09 15:55:58 +00:00
name: John Smith<br />
home: 555-555-5555<br />
cell: 666-555-5555<br />
e-mail: john@myexample.com
2005-12-10 22:09:38 +00:00
</p>
2005-05-10 21:36:53 +00:00
<p>
2006-10-09 15:55:58 +00:00
name: Jack Jones<br />
home phone: 777-555-5555<br />
cell phone: 888-555-5555<br />
e-mail: jack@myexample.com
2005-12-10 22:09:38 +00:00
</p>
2005-05-10 21:36:53 +00:00
<p>
2006-10-09 15:55:58 +00:00
name: Jane Munson<br />
home phone: 000-555-5555<br />
cell phone: 123456<br />
e-mail: jane@myexample.com
2005-12-10 22:09:38 +00:00
</p>
2005-05-10 21:36:53 +00:00
]]>
</screen>
2006-10-09 15:55:58 +00:00
</example>
2005-05-10 21:36:53 +00:00
2004-05-23 15:44:59 +00:00
<example>
2006-10-09 15:55:58 +00:00
<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 <20> Smarty.</para>
<programlisting role="php">
2005-12-10 22:09:38 +00:00
<![CDATA[
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
2006-10-09 15:55:58 +00:00
$addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');
2005-12-10 22:09:38 +00:00
$smarty->assign('address',$addr);
?>
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>La variable <parameter>loop</parameter> d<>termine uniquement le nombre
de fois qu'il faut boucler.
Vous pouvez acc<63>der <20> n'importe quelle variable du template dans la
<varname>{section}</varname></para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
{section name=customer loop=$custid}
<p>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}
</p>
2004-04-13 15:43:47 +00:00
{/section}
2005-05-10 21:36:53 +00:00
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
2006-10-09 15:55:58 +00:00
<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>
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
2005-05-10 21:36:53 +00:00
2006-10-09 15:55:58 +00:00
2004-05-23 15:44:59 +00:00
<example>
2006-10-09 15:55:58 +00:00
<title>{section} imbriqu<71>e</title>
<para>
Les sections peuvent <20>tre imbriqu<71>es autant de fois que vous le voulez.
Avec les sections imbriqu<71>es, vous pouvez acc<63>der aux structures de donn<6E>es
complexes, comme les tableaux multi-dimentionnels. Voici un script PHP qui assigne les
tableaux.
</para>
2005-12-10 22:09:38 +00:00
<programlisting role="php">
<![CDATA[
<?php
2006-10-09 15:55:58 +00:00
$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);
2005-12-10 22:09:38 +00:00
?>
2006-10-09 15:55:58 +00:00
]]>
</programlisting>
<para>Dans ce template, <emphasis>$contact_type[customer]</emphasis> est un tableau de
types de contacts.</para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
{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}
2004-04-13 15:43:47 +00:00
{/section}
2005-05-10 21:36:53 +00:00
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
2006-10-09 15:55:58 +00:00
<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 />
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
2005-05-10 21:36:53 +00:00
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
<example>
<title>Exemple avec une base de donn<6E>es et {sectionelse}</title>
<para>Les r<>sultats d'une recherche dans une base de donn<6E>es
(e.g. ADODB ou PEAR) sont assign<67>s <20> 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));
2005-12-10 22:09:38 +00:00
?>
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>Le template pour afficher le r<>sultat de la base de donn<6E>es dans un tableau HTML</para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
<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>
2004-04-13 15:43:47 +00:00
{sectionelse}
2006-10-09 15:55:58 +00:00
<tr><td colspan="5">Aucun <20>l<EFBFBD>ment n'a <20>t<EFBFBD> trouv<75></td></tr>
2005-05-10 21:36:53 +00:00
{/section}
2006-10-09 15:55:58 +00:00
</table>
2005-05-10 21:36:53 +00:00
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
2004-05-23 15:44:59 +00:00
</example>
2006-10-09 15:55:58 +00:00
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.index">
2006-10-09 15:55:58 +00:00
<title>.index</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>index</parameter> contient l'index courant du tableau, en commen<65>ant par z<>ro ou par
<parameter>start</parameter> s'il est fourni. Il s'incr<63>mente d'un en un ou de
<parameter>step</parameter> s'il est fourni.
2004-05-23 15:44:59 +00:00
</para>
<note>
<title>Note technique</title>
<para>
2006-10-09 15:55:58 +00:00
Si les propri<72>t<EFBFBD>s <parameter>step</parameter> et <parameter>start</parameter>
ne sont pas modifi<66>s, alors le fonctionnement est le m<>me que celui de la propri<72>t<EFBFBD>
<link linkend="section.property.iteration"><parameter>iteration</parameter></link>,
mise <20> part qu'il commence <20> z<>ro au lieu de un.
2004-05-23 15:44:59 +00:00
</para>
</note>
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec la propri<72>t<EFBFBD> <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>
2005-12-10 22:09:38 +00:00
<programlisting>
2005-05-10 21:36:53 +00:00
<![CDATA[
2006-10-09 15:55:58 +00:00
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
2005-05-10 21:36:53 +00:00
{/section}
]]>
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
<![CDATA[
2005-12-10 22:09:38 +00:00
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
</sect2>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.index.prev">
2006-10-09 15:55:58 +00:00
<title>.index_prev</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>index_prev</parameter> est l'index de la boucle pr<70>c<EFBFBD>dente.
Lors de la premi<6D>re boucle, il vaut -1.
2004-05-23 15:44:59 +00:00
</para>
</sect2>
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.index.next">
2006-10-09 15:55:58 +00:00
<title>.index_next</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<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.
2004-05-23 15:44:59 +00:00
</para>
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec les propri<72>t<EFBFBD>s <varname>index</varname>, <varname>index_next</varname>
et <varname>index_prev</varname></title>
<programlisting role="php">
2005-05-10 21:36:53 +00:00
<![CDATA[
2005-12-10 22:09:38 +00:00
<?php
$data = array(1001,1002,1003,1004,1005);
2006-10-09 15:55:58 +00:00
$smarty->assign('rows',$data);
2005-12-10 22:09:38 +00:00
?>
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>Le template pour afficher le tableau ci-dessus dans un tableau HTML</para>
2005-12-10 22:09:38 +00:00
<programlisting>
2006-10-09 15:55:58 +00:00
<![CDATA[
{* $rows[row.index] et $rows[row] sont identiques *}
2005-12-10 22:09:38 +00:00
<table>
2006-10-09 15:55:58 +00:00
<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}
2005-12-10 22:09:38 +00:00
</table>
2005-05-10 21:36:53 +00:00
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>
L'exemple ci-dessus affichera un tableau HTML contenant :
</para>
<screen>
2005-05-10 21:36:53 +00:00
<![CDATA[
2005-12-10 22:09:38 +00:00
index id index_prev prev_id index_next next_id
2006-10-09 15:55:58 +00:00
0 1001 -1 1 1002
2005-12-10 22:09:38 +00:00
1 1002 0 1001 2 1003
2 1003 1 1002 3 1004
3 1004 2 1003 4 1005
4 1005 3 1004 5
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
</sect2>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.iteration">
2006-10-09 15:55:58 +00:00
<title>.iteration</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>iteration</parameter> contient l'it<69>ration courante de la boucle et commence <20> un.
2004-05-23 15:44:59 +00:00
</para>
2006-10-09 15:55:58 +00:00
<note>
<para>
Ceci n'est pas affect<63> par les propri<72>t<EFBFBD>s <varname>{section}</varname>
<parameter>start</parameter>, <parameter>step</parameter> et
<parameter>max</parameter> contrairement <20> la propri<72>t<EFBFBD>
<link linkend="section.property.index"><parameter>index</parameter></link>.
<parameter>iteration</parameter> commence <20>galement <20> un au lieu de z<>ro
contrairement <20> <parameter>index</parameter>.
<link linkend="section.property.rownum"><parameter>rownum</parameter></link>
est un alias de <parameter>iteration</parameter>, ils sont identiques.
</para>
</note>
2004-05-23 15:44:59 +00:00
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec la propri<72>t<EFBFBD> <varname>iteration</varname></title>
<programlisting role="php">
2005-12-10 22:09:38 +00:00
<![CDATA[
<?php
2006-10-09 15:55:58 +00:00
// array of 3000 to 3015
2005-12-10 22:09:38 +00:00
$id = range(3000,3015);
2006-10-09 15:55:58 +00:00
$smarty->assign('arr',$id);
2005-12-10 22:09:38 +00:00
?>
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>Le template pour afficher tous les autres <20>l<EFBFBD>ments du tableau <literal>$arr</literal> comme
<literal>step=2</literal></para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
{section name=cu loop=$arr start=5 step=2}
iteration={$smarty.section.cu.iteration}
index={$smarty.section.cu.index}
id={$custid[cu]}<br />
2005-12-10 22:09:38 +00:00
{/section}
2005-05-10 21:36:53 +00:00
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
2005-05-10 21:36:53 +00:00
<![CDATA[
2005-12-10 22:09:38 +00:00
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 />
2005-05-10 21:36:53 +00:00
]]>
</screen>
2006-10-09 15:55:58 +00:00
<para>
Un autre exemple d'utilisation de la propri<72>t<EFBFBD>
<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<6F>rateur mod.
</para>
2005-12-10 22:09:38 +00:00
<programlisting>
<![CDATA[
<table>
2006-10-09 15:55:58 +00:00
{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}
2005-12-10 22:09:38 +00:00
</table>
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
</example>
2004-05-23 15:44:59 +00:00
</sect2>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.first">
2006-10-09 15:55:58 +00:00
<title>.first</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>first</parameter> est d<>fini <20> &true; si l'it<69>ration courante de
<varname>{section}</varname> est l'initiale.
2004-05-23 15:44:59 +00:00
</para>
</sect2>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.last">
2006-10-09 15:55:58 +00:00
<title>.last</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>last</parameter> est d<>fini <20> &true;
si l'it<69>ration courante de la section est la derni<6E>re.
2004-05-23 15:44:59 +00:00
</para>
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec les propri<72>t<EFBFBD>s <varname>first</varname> et <varname>last</varname></title>
2005-12-10 22:09:38 +00:00
<para>
2006-10-09 15:55:58 +00:00
Cet exemple boucle sur le tableau <varname>$customers</varname>,
affiche un bloc d'en-t<>te lors de la premi<6D>re it<69>ration et, lors de la derni<6E>re,
affiche un bloc de pied de page. Utilise aussi la propri<72>t<EFBFBD>
<link linkend="section.property.total"><parameter>total</parameter></link>.
2005-12-10 22:09:38 +00:00
</para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2005-12-10 22:09:38 +00:00
{section name=customer loop=$customers}
2006-10-09 15:55:58 +00:00
{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}
2005-12-10 22:09:38 +00:00
{/section}
2005-05-10 21:36:53 +00:00
]]>
2005-12-10 22:09:38 +00:00
</programlisting>
2004-05-23 15:44:59 +00:00
</example>
</sect2>
2005-12-10 22:09:38 +00:00
2006-10-09 15:55:58 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.rownum">
2006-10-09 15:55:58 +00:00
<title>.rownum</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>rownum</parameter> contient l'it<69>ration courante de la boucle,
commen<65>ant <20> un. C'est un alias de <link
linkend="section.property.iteration"><parameter>iteration</parameter></link>,
ils fonctionnent exactement de la m<>me fa<66>on.
2004-05-23 15:44:59 +00:00
</para>
</sect2>
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.loop">
2006-10-09 15:55:58 +00:00
<title>.loop</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>loop</parameter> contient le dernier index de la boucle de la section.
Il peut <20>tre utilis<69> dans ou apr<70>s la <varname>{section}</varname>.
</para>
2004-05-23 15:44:59 +00:00
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec la propri<72>t<EFBFBD> <varname>loop</varname></title>
2005-12-10 22:09:38 +00:00
<programlisting>
2005-05-10 21:36:53 +00:00
<![CDATA[
2006-10-09 15:55:58 +00:00
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
2005-12-10 22:09:38 +00:00
{/section}
2006-10-09 15:55:58 +00:00
There are {$smarty.section.customer.loop} customers shown above.
2005-05-10 21:36:53 +00:00
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
2005-05-10 21:36:53 +00:00
<![CDATA[
2005-12-10 22:09:38 +00:00
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
2006-10-09 15:55:58 +00:00
There are 3 customers shown above.
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
</sect2>
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.show">
2006-10-09 15:55:58 +00:00
<title>.show</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>show</parameter> est utilis<69> en tant que param<61>tre <20> la section et est une valeur bool<6F>enne.
S'il vaut &false;, la section ne sera pas affich<63>e. S'il y a un
<varname>{sectionelse}</varname>, il sera affich<63> de fa<66>on alternative.
2004-05-23 15:44:59 +00:00
</para>
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec la propri<72>t<EFBFBD> <varname>show</varname></title>
<para>Une valeur bool<6F>enne <varname>$show_customer_info</varname> est pass<73>e
depuis l'application PHP, pour r<>guler l'affichage ou non de cette section.</para>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
{section name=customer loop=$customers show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$customers[customer]}<br />
2005-12-10 22:09:38 +00:00
{/section}
2004-04-13 15:43:47 +00:00
2006-10-09 15:55:58 +00:00
{if $smarty.section.customer.show}
the section was shown.
2005-12-10 22:09:38 +00:00
{else}
2006-10-09 15:55:58 +00:00
the section was not shown.
2005-12-10 22:09:38 +00:00
{/if}
2005-05-10 21:36:53 +00:00
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
<para>
L'exemple ci-dessus affichera :
</para>
<screen>
2005-05-10 21:36:53 +00:00
<![CDATA[
2005-12-10 22:09:38 +00:00
1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />
2004-04-13 15:43:47 +00:00
2006-10-09 15:55:58 +00:00
the section was shown.
2005-05-10 21:36:53 +00:00
]]>
</screen>
2004-05-23 15:44:59 +00:00
</example>
</sect2>
2005-12-10 22:09:38 +00:00
2004-05-23 15:44:59 +00:00
<sect2 id="section.property.total">
2006-10-09 15:55:58 +00:00
<title>.total</title>
2004-05-23 15:44:59 +00:00
<para>
2006-10-09 15:55:58 +00:00
<parameter>total</parameter> contient le nombre d'it<69>rations que cette
<varname>{section}</varname> bouclera. Il peut <20>tre utilis<69> dans ou apr<70>s une
<varname>{section}</varname>.
2004-05-23 15:44:59 +00:00
</para>
<example>
2006-10-09 15:55:58 +00:00
<title>Exemple avec la propri<72>t<EFBFBD> <varname>total</varname></title>
2005-05-10 21:36:53 +00:00
<programlisting>
<![CDATA[
2006-10-09 15:55:58 +00:00
{section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
2005-12-10 22:09:38 +00:00
{/section}
2006-10-09 15:55:58 +00:00
There are {$smarty.section.customer.total} customers shown above.
2005-05-10 21:36:53 +00:00
]]>
2006-10-09 15:55:58 +00:00
</programlisting>
2004-05-23 15:44:59 +00:00
</example>
2005-05-24 20:46:11 +00:00
<para>
2005-12-10 22:09:38 +00:00
Voir aussi
2006-10-09 15:55:58 +00:00
<link linkend="language.function.foreach"><varname>{foreach}</varname></link> et
<link linkend="language.variables.smarty.loops"><parameter>$smarty.section</parameter></link>.
2005-05-24 20:46:11 +00:00
</para>
2004-05-23 15:44:59 +00:00
</sect2>
2004-04-13 15:43:47 +00:00
</sect1>
2005-12-10 22:09:38 +00:00
2004-04-13 15:43:47 +00:00
<!-- 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
2005-12-10 22:09:38 +00:00
-->