Repeating code blocks

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 allow a user 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. For more information, see Using the loop directive’s _length and _index variables.

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. Dreamweaver 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 Positioning code blocks) 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 previous 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 Builder 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>



Getting Started with Dreamweaver
Dreamweaver Basics
Working with Dreamweaver Sites
Laying Out Pages
Adding Content to Pages
Working with Page Code
Preparing to Build Dynamic Sites
Making Pages Dynamic
Defining Sources of Dynamic Content
Developing Applications Rapidly