mirror of
https://github.com/smarty-php/smarty.git
synced 2025-10-30 03:41:36 +01:00
192 lines
6.2 KiB
XML
192 lines
6.2 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- $Revision$ -->
|
|
<sect1 id="language.function.foreach">
|
|
<title>foreach,foreachelse</title>
|
|
<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>Nome Attributo</entry>
|
|
<entry>Tipo</entry>
|
|
<entry>Obbligatorio</entry>
|
|
<entry>Default</entry>
|
|
<entry>Descrizione</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>from</entry>
|
|
<entry>array</entry>
|
|
<entry>sì</entry>
|
|
<entry><emphasis>nessuno</emphasis></entry>
|
|
<entry>Array sul quale viene eseguito il ciclo</entry>
|
|
</row>
|
|
<row>
|
|
<entry>item</entry>
|
|
<entry>stringa</entry>
|
|
<entry>sì</entry>
|
|
<entry><emphasis>nessuno</emphasis></entry>
|
|
<entry>Nome della variabile che rappresenta
|
|
l'elemento attuale</entry>
|
|
</row>
|
|
<row>
|
|
<entry>key</entry>
|
|
<entry>stringa</entry>
|
|
<entry>no</entry>
|
|
<entry><emphasis>nessuno</emphasis></entry>
|
|
<entry>Nome della variabile che rappresenta la chiave attuale</entry>
|
|
</row>
|
|
<row>
|
|
<entry>name</entry>
|
|
<entry>stringa</entry>
|
|
<entry>no</entry>
|
|
<entry><emphasis>nessuno</emphasis></entry>
|
|
<entry>Nome del ciclo foreach per l'accesso alle sue proprietà</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<para>
|
|
I cicli <emphasis>foreach</emphasis> sono un'alternativa ai cicli
|
|
<emphasis>section</emphasis>. <emphasis>foreach</emphasis> si usa
|
|
per ciclare su un singolo array associativo. La sintassi di
|
|
<emphasis>foreach</emphasis> è molto più semplice di
|
|
<emphasis>session</emphasis>, ma in compenso può essere usata solo
|
|
per un array singolo. I tag <emphasis>foreach</emphasis> devono
|
|
essere chiusi con <emphasis>/foreach</emphasis>. I parametri
|
|
obbligatori sono <emphasis>from</emphasis> e <emphasis>item</emphasis>.
|
|
Il nome del ciclo foreach può essere quello che preferite, composto
|
|
di lettere, numeri e underscore. I cicli <emphasis>foreach</emphasis>
|
|
possono essere nidificati, ma i nomi dei cicli nidificati devono
|
|
essere diversi tra di loro. La variabile <emphasis>from</emphasis>
|
|
(di solito un array di valori) determina quante volte verrà eseguito
|
|
il ciclo <emphasis>foreach</emphasis>.
|
|
<emphasis>foreachelse</emphasis> viene eseguito quando non ci sono
|
|
valori nella variabile <emphasis>from</emphasis>.
|
|
</para>
|
|
<example>
|
|
<title>foreach</title>
|
|
<programlisting>
|
|
|
|
{* questo esempio stamperà tutti i valori dell'array $custid *}
|
|
{foreach from=$custid item=curr_id}
|
|
id: {$curr_id}<br>
|
|
{/foreach}
|
|
|
|
OUTPUT:
|
|
|
|
id: 1000<br>
|
|
id: 1001<br>
|
|
id: 1002<br></programlisting>
|
|
</example>
|
|
|
|
<example>
|
|
<title>foreach con key</title>
|
|
<programlisting>
|
|
{* key contiene la chiave per ogni valore del ciclo
|
|
|
|
l'assegnazione può essere qualcosa del genere:
|
|
|
|
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
|
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
|
|
|
*}
|
|
|
|
{foreach name=outer item=contact from=$contacts}
|
|
{foreach key=key item=item from=$contact}
|
|
{$key}: {$item}<br>
|
|
{/foreach}
|
|
{/foreach}
|
|
|
|
OUTPUT:
|
|
|
|
phone: 1<br>
|
|
fax: 2<br>
|
|
cell: 3<br>
|
|
phone: 555-4444<br>
|
|
fax: 555-3333<br>
|
|
cell: 760-1234<br></programlisting>
|
|
</example>
|
|
|
|
<para>
|
|
I cicli foreach hanno anche le proprie variabili che gestiscono le proprietà
|
|
del foreach. Queste vengono indicate così: {$smarty.foreach.foreachname.varname},
|
|
dove foreachname è il nome indicato come attributo <emphasis>name</emphasis>
|
|
del foreach
|
|
</para>
|
|
|
|
|
|
<sect2 id="foreach.property.iteration">
|
|
<title>iteration</title>
|
|
<para>
|
|
iteration si usa per mostrare l'iterazione corrente del ciclo.
|
|
</para>
|
|
<para>
|
|
iteration comincia sempre per 1 ed è incrementata di uno
|
|
ad ogni iterazione.
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="foreach.property.first">
|
|
<title>first</title>
|
|
<para>
|
|
<emphasis>first</emphasis> vale true quando l'iterazione attuale è la prima del ciclo.
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="foreach.property.last">
|
|
<title>last</title>
|
|
<para>
|
|
<emphasis>last</emphasis> vale true quando l'iterazione attuale è l'ultima del ciclo.
|
|
</para>
|
|
</sect2>
|
|
|
|
<sect2 id="foreach.property.show">
|
|
<title>show</title>
|
|
<para>
|
|
<emphasis>show</emphasis> si usa come parametro per il foreach.
|
|
<emphasis>show</emphasis> è un valore booleano, true o false. Quando
|
|
è false, il foreach non verrà visualizzato. Se è presente un
|
|
foreachelse, verrà visualizzato al suo posto.
|
|
</para>
|
|
|
|
</sect2>
|
|
<sect2 id="foreach.property.total">
|
|
<title>total</title>
|
|
<para>
|
|
<emphasis>total</emphasis> si usa per visualizzare il numero di iterazioni che il
|
|
ciclo foreach effettuerà. Può essere usato all'interno o dopo il foreach.
|
|
</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
|
|
-->
|