Difference between revisions of "Smarty Loops"
m |
TMMStephenY (talk | contribs) |
||
Line 5: | Line 5: | ||
''Applies to all versions of CARMA'' | ''Applies to all versions of CARMA'' | ||
− | [[Smarty]] foreach loops | + | [[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 | + | [[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: |
− | |||
− | variable for this particular loop. | ||
− | 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: |
− | |||
<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 | + | 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 == | ||
− | + | 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
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}