Difference between revisions of "Smarty Loops"

From TMM Wiki
Jump to navigationJump to search
m
 
 
Line 5: Line 5:
 
''Applies to all versions of CARMA''
 
''Applies to all versions of CARMA''
  
[[Smarty]] foreach loops know how many times they've looped so far. You
+
[[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.
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
+
[[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:
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 special [[Smarty]] variable ''.iteration'' tracks the number of times that your foreach loop has looped so far. For example:
the loop has looped so far. For example:
 
  
 
<pre>
 
<pre>
Line 20: Line 15:
 
</pre>
 
</pre>
  
Replace ''<name of foreach>'' with the name of your foreach loop. To
+
Replace ''<name of foreach>'' in the above example with the name of your foreach loop. To
name a loop, add 'name=<loop name>' to the loop declaration. For example:
+
name a loop, simply add 'name=<loop name>' to your loop declaration. For example:
  
 
<pre>
 
<pre>
Line 27: Line 22:
 
</pre>
 
</pre>
  
For example, you can insert a new table row after the fourth loop iteration using the following code:
+
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:
  
 
<pre>
 
<pre>
Line 33: Line 28:
 
</pre>
 
</pre>
  
Replace ''myloop'' with the name of your foreach loop and replace ''4''
+
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).
with the iteration number you want to act on.
 
  
 
== Example ==
 
== Example ==
This example, makes Smarty end one table row and start a new table row after every four loop iterations.
+
The following example forces [[Smarty]] to end one table row and start a new table row after every four foreach loop iterations:
  
 
<pre>
 
<pre>

Latest revision as of 15:48, 1 October 2010

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'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).

Example

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}

{/foreach}