|
Synopsis
The Discus Template Language includes a method for you to count from one number to another in a step size that you specify. The actual syntax of the 'for' command is similar to that used in the Basic programming language.
<#for $variable = start to finish step increment#>
statements...
<#endfor#>
variable is any alphanumeric string that will itself represent the value of the iteration during processing. More documentation follows.
start is the starting value of the loop.
finish is the last value of the loop if the chosen increment will cause this value to be hit exactly, or the cutoff point for processing if the chosen increment will not cause this value to be hit exactly.
increment is the amount added to start upon each successive iteration through the loop. The value of increment may be negative to count backwards. increment may not have a zero value.
Here is a count from 1 to 100:
<#for $x = 1 to 100 step 1#>
$x->{value}
<#endfor#>
1
2
...
99
100
Here is a count by 3 from 6 to 25. Note that the specified increment size of 3 and the starting value of 6 does not allow the value of 25 to be hit exactly.
<#for $x = 6 to 25 step 3#>
$x->{value}
<#endfor#>
6
9
12
15
18
21
24
Here is a backwards count by 5.5 from 10 to -7:
<#for $x = 10 to -7 step -5.5#>
$x->{value}
<#endfor#>
10
4.5
-1
-6.5
Helpful Run-time Definitions
When iterating within a FOR loop, several hashes are defined for each iteration that can assist you, especially in writing conditional code. The following table lists the hashes that are defined automatically for each iteration.
| Key |
Description |
| value |
Value of the variable |
| _iteration |
Iteration counter, starting from 1 |
| _iteration_minus1 |
Iteration counter, starting from 0 |
| _is_last_element |
1 if this is the last hit from the FOR loop, 0 otherwise |
| _is_first_element |
1 if this is the first hit from the FOR llop, 0 otherwise |
| _internal_counter |
Counter of all iterations, starting from 1 |
The following operators perform exactly as they do in a FOREACH loop:
| Code |
Description |
| <#next#> |
Skips all code between <#next#> and <#endloop#> (essentially skips processing directly ahead to the next element of the array) |
| <#last#> |
Skips all code between <#last#> and <#endloop#> and skips all code for subsequent elements (essentially stops processing of the array entirely at the point where it occurs) |
| <#skip iteration#> |
The iteration counters (_iteration and _iteration_minus1) are not incremented for this element |
Summary and further examples
Processing with a FOR loop is powerful but is much less frequently used than processing elements of an array with a FOREACH statement. As such, there are limited examples of its usefulness within the Discus templates. One example is post ratings (utilized in Discus Pro only) for skins that support them:
# $x is the post rating from 1 to 5
<img src="$html_url/icons/s1\
<#for $c = 1.25 to 4.75 step 0.50#>
{#if $x >= $c->{value}#}1{#else#}0{#endif#}\
<#endfor#>
.gif" height=12 width=60>\
The result of the above code is a HTML tag calling an image named something like s111111111.gif in the 'icons' directory. Each of these images is a series of stars from 1 to 5. Based on the actual post rating, which can range anywhere from 1 to 5, the name of the appropriate image is constructed for the browser.
|