Repeating code blocks When creating server behaviors, you can use looping constructs to repeat a code block a specified number of times. The loop syntax is:
<@ loop (@@param1@@,@@param2@@,@@param3@@,@@param_n@@) @>
code block
<@ endloop @>
The loop directive accepts a comma-separated list of parameter arrays as arguments.
In this case, parameter array arguments allows a users to supply multiple values
for a single parameter. The repeating text will be duplicated n times, where
n is the length of the parameter array arguments. If more than one parameter
array argument is specified, all the arrays must have the same length. On the
ith evaluation of the loop, the ith elements of the parameter arrays replace
the associated parameter instances in the code block.
When you later create a dialog box for the server behavior (see Creating a dialog box for a custom server behavior), you can add a control to the dialog box that allows the page designer to create parameter arrays. includes a simple array control that you can use to create dialog boxes. This control, called Text Field Comma Separated List, is available through the Server Behavior builder. To create user interface elements of greater complexity, see the API documentation to create a dialog box with a control to create arrays (a grid control, for example).
Loop directives cannot be nested, but conditional directives (see Making code blocks conditional) can be nested within a loop directive.
The following example shows how such repeating code blocks can be used to create server behaviors (the example is a ColdFusion behavior used to access a stored procedure):
<CFSTOREDPROC procedure="AddNewBook"
datasource=#MM_connection_DSN#
username=#MM_connection_USERNAME#
password=#MM_connection_PASSWORD#>
<CFPROCPARAM type="IN" dbvarname="@CategoryId" value="#Form.CategoryID#"
cfsqltype="CF_SQL_INTEGER">
<CFPROCPARAM type="IN" dbvarname="@ISBN" value="#Form.ISBN#"
cfsqltype="CF_SQL_VARCHAR">
</CFSTOREDPROC>
In this example, the CFSTOREDPROC tag can include zero or more CFPROCPARAM tags.
However, without support for the loop directive, there is no way to include
the CFPROCPARAM tags within the inserted CFSTOREDPROC tag. If this were to be
created as a server behavior without the use of the loop directive, you would
need to divide this example into two participants: a main CFSTOREDPROC tag,
and a CFPROCPARAM tag whose participant type is multiple.
Using the loop directive, the same procedure can be written as follows:
<CFSTOREDPROC procedure="@@procedure@@"
datasource=#MM_@@conn@@_DSN#
username=#MM_@@conn@@_USERNAME#
password=#MM_@@conn@@_PASSWORD#>
<@ loop (@@paramName@@,@@value@@,@@type@@) @>
<CFPROCPARAM type="IN"
dbvarname="@@paramName@@"
value="@@value@@"
cfsqltype="@@type@@">
<@ endloop @>
</CFSTOREDPROC>
In the above example, and in the case of conditional code blocks as well, newlines
after @> are ignored.
If the user entered the following parameter values in the server behavior dialog box:
procedure = "proc1"
conn = "connection1"
paramName = ["@CategoryId", "@Year", "@ISBN"]
value = ["#Form.CategoryId#", "#Form.Year#", "#Form.ISBN#"]
type = ["CF_SQL_INTEGER", "CF_SQL_INTEGER", "CF_SQL_VARCHAR"]
The server behavior would insert the following runtime code in the page:
<CFSTOREDPROC procedure="proc1"
datasource=#MM_connection1_DSN#
username=#MM_connection1_USERNAME#
password=#MM_connection1_PASSWORD#>
<CFPROCPARAM type="IN" dbvarname="@CategoryId" value="#Form.CategoryId#"
cfsqltype="CF_SQL_INTEGER">
<CFPROCPARAM type="IN" dbvarname="@Year" value="#Form.Year#"
cfsqltype="CF_SQL_INTEGER">
<CFPROCPARAM type="IN" dbvarname="@ISBN" value="#Form.ISBN#"
cfsqltype="CF_SQL_VARCHAR">
</CFSTOREDPROC>
Note: Parameter arrays cannot be used outside of a loop except as part of a
conditional directive expression.
1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101