Smarty Loops

From TMMwiki
Jump to: navigation, search
Templates Admin
Members Area Templates
Tour Templates
Member Favorites
Smarty Loops
Smarty Caching
Smarty print array
Base Templates
User Variables
Rating System

Applies to all versions of CARMA

Smarty's foreach loops are able to keep track of how many times they have looped so far. Using this knowledge, you can set CARMA to perform a task for every two or more iterations of the foreach loop.

Smarty uses the name of each foreach loop to track how many iterations it has looped through. You can access this information by using the reserved Smarty variable for this particular loop. To do so, use the following format:

The special Smarty variable .iteration tracks the number of times that your foreach loop has looped so far. For example:

$smarty.foreach.<name of foreach>.iteration

Replace <name of foreach> in the above example with the name of your foreach loop. To name a loop, simply add 'name=<loop name>' to your loop declaration. For example:

{foreach from=$carma_video.files.$type key=id item=file name=myloop}

Using foreach loops and the .iteration variable, you can set tasks to be performed after a certain amount of loops. For example, you can insert a new table row after the fourth loop iteration by using the sample following code:

{if ($smarty.foreach.myloop.iteration) % 4 == 0}</tr><tr>{/if}

Replace myloop in the above example with the name of your foreach loop, and replace 4 in the above example with the iteration number you want to act on (how many times foreach has looped).


The following example forces Smarty to end one table row and start a new table row after every four foreach 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}