Difference between revisions of "Smarty Loops"
m |
(No difference)
|
Revision as of 13:44, 23 September 2008
Applies to all versions of CARMA
Smarty foreach loops know how many times they've looped so far. You can use this knowledge to perform a task every two or more loop iterations.
Smarty tracks how many iterations there are for all foreach loops according to their name. This can be accessed via the reserved Smarty variable for this particular loop. The format to do so is as follows:
The special Smarty variable .iteration tracks the number of times the loop has looped so far. For example:
$smarty.foreach.<name of foreach>.iteration
Replace <name of foreach> with the name of your foreach loop. To name a loop, add 'name=<loop name>' to the loop declaration. For example:
{foreach from=$carma_video.files.$type key=id item=file name=myloop}
For example, you can insert a new table row after the fourth loop iteration using the following code:
{if ($smarty.foreach.myloop.iteration) % 4 == 0}</tr><tr>{/if}
Replace myloop with the name of your foreach loop and replace 4 with the iteration number you want to act on.
Example
This example, makes Smarty end one table row and start a new table row after every four loop iterations.
{foreach from=$carma_video.files.$type key=id item=file name=myloop} <!-- Code to display the videos with whatever HTML formatting you wish --> {if ($smarty.foreach.myloop.iteration) % 4== 0}</tr><tr>{/if} {/foreach}