Les boucles
Exemple de code PHP :
$product_list = array(
23 => array('no' => 2456, 'label' => 'Salad'),
96 => array('no' => 4889, 'label' => 'Cream')
);
$smarty->assign('products', $product_list);
Utilisation dans smarty
<ul>
{foreach from=$products item=product}
<li><a href="product.php?id={$product.no}">{$product.label}</li>
{/foreach}
</ul>
Il est possible de connaître l'indice de l'enregistrement dans une boucle avec $smarty.foreach suivi du nom de la boucle.
Exemple :
{foreach from=$products key=id_product item=product name=listproduct}
{if $smarty.foreach.listproduct.first}Premier{/if}
{if $smarty.foreach.listproduct.last}Dernier{/if}
Index : {$smarty.foreach.listproduct.index}
{if $smarty.foreach.listproduct.index % 2}Ligne paire{/if}
{/foreach}