Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

section,sectionelse

Nome Attributo Tipo Obbligatorio Default Descrizione
name stringa nessuno Nome della sezione
loop [$variable_name] nessuno Nome della variabile che determina il numero di iterazioni del ciclo
start intero no 0 L'indice dal quale inizierà il ciclo. Se il valore è negativo, la posizione di partenza è calcolata dalla fine dell'array. Ad esempio, se ci sono sette valori nell'array da ciclare e start è -2, l'indice di partenza sarà 5. Valori non validi (cioè al di fuori della lunghezza dell'array da ciclare) saranno automaticamente convertiti al valore valido più vicino.
step intero no 1 Il valore di passo da usare per attraversare l'array da ciclare. Ad esempio, step=2 ciclerà sugli indici 0,2,4, ecc. Se step è negativo il ciclo procederà sull'array all'indietro.
max intero no nessuno Massimo numero di cicli per la sezione.
show booleano no true Stabilisce se mostrare o no la sezione

Le sezioni sono usate per ciclare su array di dati. Tutti i tag section devono essere chiusi con /section. I parametri obbligatori sono name e loop. Il nome della sezione può essere quello che preferite, formato da lettere, numeri e underscore. Le sezioni possono essere nidificate, ed i nomi delle sezioni nidificate devono essere diversi fra loro. La variabile loop (di solito un array di valori) determina quante volte sarà eseguito il ciclo. Quando stampate una variabile all'interno di una sezione, il nome della sezione deve essere indicato a fianco del nome della variabile fra parentesi quadre []. sectionelse viene eseguito quando non ci sono valori nella variabile loop.

Example 7.15. section


{* questo esempio stamperà tutti i valori dell'array $custid *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
{/section}

OUTPUT:

id: 1000<br>
id: 1001<br>
id: 1002<br>

Example 7.16. variabile loop

{* la variabile loop determina soltanto il numero di cicli da ripetere.
   Potete accedere a qualsiasi variabile dal template della sezione.
   In questo esempio presumiamo che $custid, $name e $address siano
   tutti array contenenti lo stesso numero di valori *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
	name: {$name[customer]}<br>
	address: {$address[customer]}<br>
	<p>
{/section}


OUTPUT:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p>

Example 7.17. nomi delle sezioni

{* come nome della sezione potete usare quello che preferite,
   e viene usato per riferirsi ai dati all'interno della sezione *}
{section name=mydata loop=$custid}
	id: {$custid[mydata]}<br>
	name: {$name[mydata]}<br>
	address: {$address[mydata]}<br>
	<p>
{/section}

Example 7.18. sezioni nidificate

{* le sezioni possono essere nidificate a qualsiasi profondità. Con
   le sezioni nidificate potete accedere a strutture di dati complesse,
   ad esempio array multidimensionali. In questo esempio, $contact_type[customer]
   è un array di tipi di contatto per il cliente corrente. *}
{section name=customer loop=$custid}
	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}
	<p>
{/section}


OUTPUT:

id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@myexample.com<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@myexample.com<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@myexample.com<br>
<p>

Example 7.19. sezioni e array associativi

{* questo è un esempio di stampa di un array associativo
   di dati in una sezione *}
{section name=customer loop=$contacts}
	name: {$contacts[customer].name}<br>
	home: {$contacts[customer].home}<br>
	cell: {$contacts[customer].cell}<br>
	e-mail: {$contacts[customer].email}<p>
{/section}


OUTPUT:

name: John Smith<br>
home: 555-555-5555<br>
cell: 555-555-5555<br>
e-mail: john@myexample.com<p>
name: Jack Jones<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@myexample.com<p>
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@myexample.com<p>

Example 7.20. sectionelse

{* sectionelse viene eseguito se non ci sono valori in $custid *}
{section name=customer loop=$custid}
	id: {$custid[customer]}<br>
{sectionelse}
	there are no values in $custid.
{/section}

Le sezioni hanno anche le proprie variabili di gestione delle proprietà. Vengono indicate così: {$smarty.section.nomesezione.nomevariabile}

Note

A partire da Smarty 1.5.0, la sintassi per le variabili delle proprietà di sessione è cambiata da {%nomesezione.nomevariabile%} a {$smarty.section.sectionname.varname}. La vecchia sintassi è ancora supportata, ma negli esempi del manuale troverete solo riferimenti alla nuova.

index

index si usa per visualizzare l'attuale indice del ciclo, partendo da zero (o dall'attributo start se presente), e con incrementi di uno (o dell'attributo step se presente).

Nota tecnica

Se le proprietà step e start non vengono modificate, index funziona allo stesso modo della proprietà iteration, ad eccezione del fatto che parte da 0 invece che da 1.

Example 7.21. proprietà index

	{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{/section}


	OUTPUT:

	0 id: 1000<br>
	1 id: 1001<br>
	2 id: 1002<br>

index_prev

index_prev visualizza l'indice del ciclo precedente. Sul primo ciclo è impostata a -1.

Example 7.22. proprietà index_prev

	{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{* nota: $custid[customer.index] e $custid[customer] hanno identico significato *}
	{if $custid[customer.index_prev] ne $custid[customer.index]}
    	The customer id changed<br>
	{/if}
	{/section}


	OUTPUT:

	0 id: 1000<br>
    	The customer id changed<br>
	1 id: 1001<br>
    	The customer id changed<br>
	2 id: 1002<br>
    	The customer id changed<br>

index_next

index_next visualizza l'indice del prossimo ciclo. Sull'ultimo ciclo ha sempre il valore maggiore dell'attuale (rispettando l'attributo step, quando presente).

Example 7.23. proprietà index_next

	{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{* nota: $custid[customer.index] e $custid[customer] hanno identico significato *}
	{if $custid[customer.index_next] ne $custid[customer.index]}
    	The customer id will change<br>
	{/if}
	{/section}


	OUTPUT:

	0 id: 1000<br>
    	The customer id will change<br>
	1 id: 1001<br>
    	The customer id will change<br>
	2 id: 1002<br>
    	The customer id will change<br>

iteration

iteration visualizza l'iterazione attuale del ciclo.

Note

Al contrario di index, questa proprietà non è influenzata dalle proprietà start, step e max. Inoltre iteration comincia da 1 invece che da 0 come index. rownum è un alias di iteration, e funziona in modo identico.

Example 7.24. proprietà iteration

	{section name=customer loop=$custid start=5 step=2}
	current loop iteration: {$smarty.section.customer.iteration}<br>
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{* nota: $custid[customer.index] e $custid[customer] hanno identico significato *}
	{if $custid[customer.index_next] ne $custid[customer.index]}
    	The customer id will change<br>
	{/if}
	{/section}


	OUTPUT:

	current loop iteration: 1
	5 id: 1000<br>
    	The customer id will change<br>
	current loop iteration: 2
	7 id: 1001<br>
    	The customer id will change<br>
	current loop iteration: 3
	9 id: 1002<br>
    	The customer id will change<br>

first

first vale true se l'iterazione attuale è la prima.

Example 7.25. proprietà first

	{section name=customer loop=$custid}
	{if $smarty.section.customer.first}
    	<table>
	{/if}

	<tr><td>{$smarty.section.customer.index} id:
        	{$custid[customer]}</td></tr>

	{if $smarty.section.customer.last}
    	</table>
	{/if}
	{/section}


	OUTPUT:

	<table>
	<tr><td>0 id: 1000</td></tr>
	<tr><td>1 id: 1001</td></tr>
	<tr><td>2 id: 1002</td></tr>
	</table>

last

last vale true se l'attuale iterazione è l'ultima.

Example 7.26. proprietà last

	{section name=customer loop=$custid}
	{if $smarty.section.customer.first}
    	<table>
	{/if}

	<tr><td>{$smarty.section.customer.index} id:
        	{$custid[customer]}</td></tr>

	{if $smarty.section.customer.last}
    	</table>
	{/if}
	{/section}


	OUTPUT:

	<table>
	<tr><td>0 id: 1000</td></tr>
	<tr><td>1 id: 1001</td></tr>
	<tr><td>2 id: 1002</td></tr>
	</table>

rownum

rownum visualizza l'iterazione attuale del ciclo, partendo da uno. E' un alias di iteration, e funziona in modo identico.

Example 7.27. proprietà rownum

	{section name=customer loop=$custid}
	{$smarty.section.customer.rownum} id: {$custid[customer]}<br>
	{/section}


	OUTPUT:

	1 id: 1000<br>
	2 id: 1001<br>
	3 id: 1002<br>

loop

loop visualizza l'index dell'ultimo ciclo visualizzato dalla sezione. Può essere usato all'interno o dopo la sezione.

Example 7.28. proprietà index

	{section name=customer loop=$custid}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{/section}

	There were {$smarty.section.customer.loop} customers shown above.

	OUTPUT:

	0 id: 1000<br>
	1 id: 1001<br>
	2 id: 1002<br>

	There were 3 customers shown above.

show

show è usato come parametro per la sezione. show è un valore booleano, true o false. Se false, la sezione non verrà visualizzata. Se è presente un sectionelse, verrà visualizzato questo.

Example 7.29. attributo show

	{* $show_customer_info potrebbe essere stato passato dall'applicazione
        PHP, per stabilire se questa sezione deve essere visualizzata o no *}
	{section name=customer loop=$custid show=$show_customer_info}
	{$smarty.section.customer.rownum} id: {$custid[customer]}<br>
	{/section}

	{if $smarty.section.customer.show}
	the section was shown.
	{else}
	the section was not shown.
	{/if}


	OUTPUT:

	1 id: 1000<br>
	2 id: 1001<br>
	3 id: 1002<br>

	the section was shown.

total

total visualizza il numero totale di iterazioni che la sezione eseguirà. Può essere usato all'interno o dopo la sezione.

Example 7.30. proprietà total

	{section name=customer loop=$custid step=2}
	{$smarty.section.customer.index} id: {$custid[customer]}<br>
	{/section}

	There were {$smarty.section.customer.total} customers shown above.

	OUTPUT:

	0 id: 1000<br>
	2 id: 1001<br>
	4 id: 1002<br>

	There were 3 customers shown above.

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors