Smarty Loops

From TMM Wiki
Revision as of 13:44, 23 September 2008 by Trinidadr (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
CARMA
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 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}