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"
<CFPROCPARAM type="IN" dbvarname="@CategoryId" value="#Form.CategoryID#"
<CFPROCPARAM type="IN" dbvarname="@ISBN" value="#Form.ISBN#"

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@@"
<@ loop (@@paramName@@,@@value@@,@@type@@) @>
   <CFPROCPARAM type="IN" 
<@ endloop @>

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#"]

The server behavior would insert the following runtime code in the page:

<CFSTOREDPROC procedure="proc1"

<CFPROCPARAM type="IN" dbvarname="@CategoryId" value="#Form.CategoryId#"
<CFPROCPARAM type="IN" dbvarname="@Year" value="#Form.Year#"
<CFPROCPARAM type="IN" dbvarname="@ISBN" value="#Form.ISBN#"

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