PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/documentation/fr/designers/language-builtin-functions/language-function-foreach.xml

https://bitbucket.org/hallgrennetworks/smarty
XML | 457 lines | 415 code | 20 blank | 22 comment | 0 complexity | 100efdb1bd68d73c855d9dd1bff32414 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- $Revision: 2972 $ -->
  3. <!-- EN-Revision: 1.11 Maintainer: gerald Status: ready -->
  4. <sect1 id="language.function.foreach">
  5. <title>{foreach},{foreachelse}</title>
  6. <para>
  7. <varname>{foreach}</varname> est utilisé pour parcourir un
  8. <emphasis role="bold">simple tableau associatif</emphasis>,
  9. contrairement à <link linkend="language.function.section"><varname>{section}</varname></link>
  10. qui effectue une boucle sur les <emphasis role="bold">tableaux de données</emphasis>.
  11. La synthaxe pour
  12. <varname>{foreach}</varname> est plus simple que
  13. <link linkend="language.function.section"><varname>{section}</varname></link>,
  14. mais <emphasis role="bold">ne peut être utilisé que pour des tableau simple</emphasis>.
  15. Chaque <varname>{foreach}</varname> doit aller de paire avec une balise fermante
  16. <varname>{/foreach}</varname>.
  17. </para>
  18. <informaltable frame="all">
  19. <tgroup cols="5">
  20. <colspec colname="param" align="center" />
  21. <colspec colname="type" align="center" />
  22. <colspec colname="required" align="center" />
  23. <colspec colname="default" align="center" />
  24. <colspec colname="desc" />
  25. <thead>
  26. <row>
  27. <entry>Nom attribut</entry>
  28. <entry>Type</entry>
  29. <entry>Requis</entry>
  30. <entry>Defaut</entry>
  31. <entry>Description</entry>
  32. </row>
  33. </thead>
  34. <tbody>
  35. <row>
  36. <entry>from</entry>
  37. <entry>tableau</entry>
  38. <entry>oui</entry>
  39. <entry><emphasis>n/a</emphasis></entry>
  40. <entry>Le tableau à parcourir</entry>
  41. </row>
  42. <row>
  43. <entry>item</entry>
  44. <entry>chaîne de caractère</entry>
  45. <entry>Oui</entry>
  46. <entry><emphasis>n/a</emphasis></entry>
  47. <entry>Le nom de la variable "élément courant"</entry>
  48. </row>
  49. <row>
  50. <entry>key</entry>
  51. <entry>chaîne de caractère</entry>
  52. <entry>Non</entry>
  53. <entry><emphasis>n/a</emphasis></entry>
  54. <entry>Le nom de la variable représentant la clef courante.</entry>
  55. </row>
  56. <row>
  57. <entry>name</entry>
  58. <entry>chaîne de caractère</entry>
  59. <entry>Non</entry>
  60. <entry><emphasis>n/a</emphasis></entry>
  61. <entry>Le nom de la boucle foreach, qui nous permettra
  62. d'accéder à ses propriétés.</entry>
  63. </row>
  64. </tbody>
  65. </tgroup>
  66. </informaltable>
  67. <itemizedlist>
  68. <listitem><para>
  69. Required attributes are <parameter>from</parameter> and <parameter>item</parameter>.
  70. </para></listitem>
  71. <listitem><para>
  72. The <parameter>name</parameter> of the <varname>{foreach}</varname> loop can be anything
  73. you like, made up of letters, numbers and underscores, like
  74. <ulink url="&url.php-manual;language.variables">PHP variables</ulink>.
  75. </para></listitem>
  76. <listitem><para>
  77. <varname>{foreach}</varname> loops can be nested, and the nested
  78. <varname>{foreach}</varname> names must be unique from each other.
  79. </para></listitem>
  80. <listitem><para>
  81. The <parameter>from</parameter> attribute, usually an array of values,
  82. determines the number of times <varname>{foreach}</varname> will loop.
  83. </para></listitem>
  84. <listitem><para>
  85. <varname>{foreachelse}</varname> is executed when there are no
  86. values in the <parameter>from</parameter> variable.
  87. </para></listitem>
  88. <listitem><para>
  89. <varname>{foreach}</varname> loops also have their own variables that handle properties.
  90. These are accessed with:
  91. <link linkend="language.variables.smarty.loops">
  92. <parameter>{$smarty.foreach.name.property}</parameter></link> with
  93. <quote>name</quote> being the
  94. <parameter>name</parameter> attribute.
  95. </para>
  96. <note>
  97. <title>Note</title>
  98. <para>The <parameter>name</parameter> attribute is only required when
  99. you want to access a <varname>{foreach</varname>} property, unlike
  100. <link linkend="language.function.section"><varname>{section}</varname></link>.
  101. Accessing a <varname>{foreach}</varname> property with <parameter>name</parameter>
  102. undefined does not throw an error, but leads to unpredictable results instead.
  103. </para>
  104. </note>
  105. </listitem>
  106. <listitem><para>
  107. <varname>{foreach}</varname> properties are
  108. <link linkend="foreach.property.index"><parameter>index</parameter></link>,
  109. <link linkend="foreach.property.iteration"><parameter>iteration</parameter></link>,
  110. <link linkend="foreach.property.first"><parameter>first</parameter></link>,
  111. <link linkend="foreach.property.last"><parameter>last</parameter></link>,
  112. <link linkend="foreach.property.show"><parameter>show</parameter></link>,
  113. <link linkend="foreach.property.total"><parameter>total</parameter></link>.
  114. </para></listitem>
  115. </itemizedlist>
  116. <example>
  117. <title>L'attribut <parameter>item</parameter></title>
  118. <programlisting role="php">
  119. <![CDATA[
  120. <?php
  121. $arr = array(1000, 1001, 1002);
  122. $smarty->assign('myArray', $arr);
  123. ?>
  124. ]]>
  125. </programlisting>
  126. <para>
  127. Template pour afficher <parameter>$myArray</parameter> dans une liste non-ordonnée.
  128. </para>
  129. <programlisting>
  130. <![CDATA[
  131. <ul>
  132. {foreach from=$myArray item=foo}
  133. <li>{$foo}</li>
  134. {/foreach}
  135. </ul>
  136. ]]>
  137. </programlisting>
  138. <para>
  139. L'exemple ci-dessus affichera :
  140. </para>
  141. <screen>
  142. <![CDATA[
  143. <ul>
  144. <li>1000</li>
  145. <li>1001</li>
  146. <li>1002</li>
  147. </ul>
  148. ]]>
  149. </screen>
  150. </example>
  151. <example>
  152. <title>Utilisation des attributs <parameter>item</parameter> et <parameter>key</parameter></title>
  153. <programlisting role="php">
  154. <![CDATA[
  155. <?php
  156. $arr = array(9 => 'Tennis', 3 => 'Natation', 8 => 'Programmation');
  157. $smarty->assign('myArray', $arr);
  158. ?>
  159. ]]>
  160. </programlisting>
  161. <para>
  162. Le template affiche le tableau <parameter>$myArray</parameter> comme paire clé/valeur,
  163. comme la fonction PHP
  164. <ulink url="&url.php-manual;foreach"><varname>foreach</varname></ulink>.
  165. </para>
  166. <programlisting>
  167. <![CDATA[
  168. <ul>
  169. {foreach from=$myArray key=k item=v}
  170. <li>{$k}: {$v}</li>
  171. {/foreach}
  172. </ul>
  173. ]]>
  174. </programlisting>
  175. <para>
  176. L'exemple ci-dessus affichera :
  177. </para>
  178. <screen>
  179. <![CDATA[
  180. <ul>
  181. <li>9: Tennis</li>
  182. <li>3: Natation</li>
  183. <li>8: Programmation</li>
  184. </ul>
  185. ]]>
  186. </screen>
  187. </example>
  188. <example>
  189. <title>{foreach} avec un attribut associatif <parameter>item</parameter></title>
  190. <programlisting role="php">
  191. <![CDATA[
  192. <?php
  193. $items_list = array(23 => array('no' => 2456, 'label' => 'Salad'),
  194. 96 => array('no' => 4889, 'label' => 'Cream')
  195. );
  196. $smarty->assign('items', $items_list);
  197. ?>
  198. ]]>
  199. </programlisting>
  200. <para>
  201. Le template affiche <parameter>$items</parameter> avec
  202. <parameter>$myId</parameter> dans l'URL.
  203. </para>
  204. <programlisting>
  205. <![CDATA[
  206. <ul>
  207. {foreach from=$items key=myId item=i}
  208. <li><a href="item.php?id={$myId}">{$i.no}: {$i.label}</li>
  209. {/foreach}
  210. </ul>
  211. ]]>
  212. </programlisting>
  213. <para>
  214. L'exemple ci-dessus affichera :
  215. </para>
  216. <screen>
  217. <![CDATA[
  218. <ul>
  219. <li><a href="item.php?id=23">2456: Salad</li>
  220. <li><a href="item.php?id=96">4889: Cream</li>
  221. </ul>
  222. ]]>
  223. </screen>
  224. </example>
  225. <example>
  226. <title>{foreach} avec <parameter>item</parameter> et <parameter>key</parameter></title>
  227. <para>Assigne un tableau à Smarty, la clé contient la clé pour chaque valeur de la boucle.</para>
  228. <programlisting role="php">
  229. <![CDATA[
  230. <?php
  231. $smarty->assign('contacts', array(
  232. array('phone' => '1',
  233. 'fax' => '2',
  234. 'cell' => '3'),
  235. array('phone' => '555-4444',
  236. 'fax' => '555-3333',
  237. 'cell' => '760-1234')
  238. ));
  239. ?>
  240. ]]>
  241. </programlisting>
  242. <para>Le template affiche <parameter>$contact</parameter>.</para>
  243. <programlisting>
  244. <![CDATA[
  245. {foreach name=outer item=contact from=$contacts}
  246. <hr />
  247. {foreach key=key item=item from=$contact}
  248. {$key}: {$item}<br />
  249. {/foreach}
  250. {/foreach}
  251. ]]>
  252. </programlisting>
  253. <para>
  254. L'exemple ci-dessus affichera :
  255. </para>
  256. <screen>
  257. <![CDATA[
  258. <hr />
  259. phone: 1<br />
  260. fax: 2<br />
  261. cell: 3<br />
  262. <hr />
  263. phone: 555-4444<br />
  264. fax: 555-3333<br />
  265. cell: 760-1234<br />
  266. ]]>
  267. </screen>
  268. </example>
  269. <example>
  270. <title>Exemple d'une base de données avec {foreachelse}</title>
  271. <para>Exemple d'un script de recherche dans une base de données (e.g. PEAR ou ADODB),
  272. le résultat de la requête est assigné à Smarty.</para>
  273. <programlisting role="php">
  274. <![CDATA[
  275. <?php
  276. $search_condition = "where name like '$foo%' ";
  277. $sql = 'select contact_id, name, nick from contacts '.$search_condition.' order by name';
  278. $smarty->assign('results', $db->getAssoc($sql) );
  279. ?>
  280. ]]>
  281. </programlisting>
  282. <para>Le template qui affiche <quote>None found</quote>
  283. si aucun résultat avec <varname>{foreachelse}</varname>.</para>
  284. <programlisting>
  285. <![CDATA[
  286. {foreach key=cid item=con from=$results}
  287. <a href="contact.php?contact_id={$cid}">{$con.name} - {$con.nick}</a><br />
  288. {foreachelse}
  289. Aucun élément n'a été trouvé dans la recherche
  290. {/foreach}
  291. ]]>
  292. </programlisting>
  293. </example>
  294. <sect2 id="foreach.property.index">
  295. <title>.index</title>
  296. <para>
  297. <parameter>index</parameter> contient l'index courant du tableau, en commançant par zéro.
  298. </para>
  299. <example>
  300. <title>Exemple avec <parameter>index</parameter></title>
  301. <programlisting role="php">
  302. <![CDATA[
  303. {* L'en-tête du block est affiché toutes les 5 lignes *}
  304. <table>
  305. {foreach from=$items key=myId item=i name=foo}
  306. {if $smarty.foreach.foo.index % 5 == 0}
  307. <tr><th>Title</th></tr>
  308. {/if}
  309. <tr><td>{$i.label}</td></tr>
  310. {/foreach}
  311. </table>
  312. ]]>
  313. </programlisting>
  314. </example>
  315. </sect2>
  316. <sect2 id="foreach.property.iteration">
  317. <title>.iteration</title>
  318. <para>
  319. <parameter>iteration</parameter> contient l'itération courante de la boucle et commence
  320. toujours à 1, contrairement à
  321. <link linkend="foreach.property.index"><parameter>index</parameter></link>.
  322. Il est incrémenté d'un, à chaque itération.
  323. </para>
  324. <example>
  325. <title>Exemple avec <parameter>iteration</parameter> et <parameter>index</parameter></title>
  326. <programlisting role="php">
  327. <![CDATA[
  328. {* this will output 0|1, 1|2, 2|3, ... etc *}
  329. {foreach from=$myArray item=i name=foo}
  330. {$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
  331. {/foreach}
  332. ]]>
  333. </programlisting>
  334. </example>
  335. </sect2>
  336. <sect2 id="foreach.property.first">
  337. <title>.first</title>
  338. <para>
  339. <parameter>first</parameter> vaut &true; si l'itération courante de
  340. <varname>{foreach}</varname> est l'initial.
  341. </para>
  342. <example>
  343. <title>Exemple avec <parameter>first</parameter></title>
  344. <programlisting role="php">
  345. <![CDATA[
  346. {* affiche LATEST sur le premier élément, sinon, l'id *}
  347. <table>
  348. {foreach from=$items key=myId item=i name=foo}
  349. <tr>
  350. <td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
  351. <td>{$i.label}</td>
  352. </tr>
  353. {/foreach}
  354. </table>
  355. ]]>
  356. </programlisting>
  357. </example>
  358. </sect2>
  359. <sect2 id="foreach.property.last">
  360. <title>.last</title>
  361. <para>
  362. <parameter>last</parameter> est défini à &true; si l'itération courante de
  363. <varname>{foreach}</varname> est la dernière.
  364. </para>
  365. <example>
  366. <title>Exemple avec <parameter>last</parameter></title>
  367. <programlisting role="php">
  368. <![CDATA[
  369. {* Ajout une barre horizontale à la fin de la liste *}
  370. {foreach from=$items key=part_id item=prod name=products}
  371. <a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if}
  372. {foreachelse}
  373. ... contenu ...
  374. {/foreach}
  375. ]]>
  376. </programlisting>
  377. </example>
  378. </sect2>
  379. <sect2 id="foreach.property.show">
  380. <title>.show</title>
  381. <para>
  382. <parameter>show</parameter> est utilisé en tant que paramètre à <varname>{foreach}</varname>.
  383. <parameter>show</parameter> est une valeur booléenne. S'il vaut
  384. &false;, <varname>{foreach}</varname> ne sera pas affiché.
  385. S'il y a un <varname>{foreachelse}</varname>, il sera affiché alternativement.
  386. </para>
  387. </sect2>
  388. <sect2 id="foreach.property.total">
  389. <title>.total</title>
  390. <para>
  391. <parameter>total</parameter> contient le nombre d'itérations que cette boucle
  392. <varname>{foreach}</varname> effectuera.
  393. Il peut être utilisé dans ou après un <varname>{foreach}</varname>.
  394. </para>
  395. <example>
  396. <title>Exemple avec <parameter>total</parameter></title>
  397. <programlisting role="php">
  398. <![CDATA[
  399. {* affiche les lignes retournées à la fin *}
  400. {foreach from=$items key=part_id item=prod name=foo}
  401. {$prod.name><hr/>
  402. {if $smarty.foreach.foo.last}
  403. <div id="total">{$smarty.foreach.foo.total} items</div>
  404. {/if}
  405. {foreachelse}
  406. ... quelque chose d'autre ...
  407. {/foreach}
  408. ]]>
  409. </programlisting>
  410. </example>
  411. <para>
  412. Voir aussi
  413. <link linkend="language.function.section"><varname>{section}</varname></link>
  414. et <link linkend="language.variables.smarty.loops"><parameter>$smarty.foreach</parameter></link>.
  415. </para>
  416. </sect2>
  417. </sect1>
  418. <!-- Keep this comment at the end of the file
  419. Local variables:
  420. mode: sgml
  421. sgml-omittag:t
  422. sgml-shorttag:t
  423. sgml-minimize-attributes:nil
  424. sgml-always-quote-attributes:t
  425. sgml-indent-step:1
  426. sgml-indent-data:t
  427. indent-tabs-mode:nil
  428. sgml-parent-document:nil
  429. sgml-default-dtd-file:"../../../../manual.ced"
  430. sgml-exposed-tags:nil
  431. sgml-local-catalogs:nil
  432. sgml-local-ecat-files:nil
  433. End:
  434. vim600: syn=xml fen fdm=syntax fdl=2 si
  435. vim: et tw=78 syn=sgml
  436. vi: ts=1 sw=1
  437. -->