The code blocks you create in the Server Behavior Builder are encapsulated in a server behavior that appears in the Server Behaviors panel. The code can be any valid run-time code for the specified server model. For example, if you choose ColdFusion as the document type for your custom server behavior, the code you write must be valid ColdFusion code that runs on a ColdFusion application server.
You can create the code blocks either directly in the Server Behavior Builder, or you can copy and paste the code from other sources. Each code block you create in the Server Behavior Builder must be a single tag or script block. If you must insert multiple tag blocks, split them into separate code blocks.
Dreamweaver lets you develop code blocks that incorporate control statements that execute conditionally. The Server Behavior Builder uses if, elseif, and else statements, and may also contain server behavior parameters. This enables you to insert alternate text blocks based on the values of OR relationships among server behavior parameters.
The following example shows the if, elseif, and else statements. The square brackets ([ ]) denote optional code and the asterisk (*) denotes zero or more instances. To execute a portion of a code block or the entire code block only if a certain condition or conditions apply, use the following syntax:
<@ if (expression1) @> conditional text1[<@ elseif (expression2) @> conditional text2]*[<@ else @> conditional text3]<@ endif @>
Condition expressions can be any JavaScript expression that can be evaluated using the JavaScript eval() function, and may include a server behavior parameter marked by @@’s. (The @@’s distinguish the parameter from JavaScript variables and keywords.)
When using if, else, and elseif directives within the insertText XML tag, the participant text is preprocessed to resolve the if directives and to determine which text to include in the result. The if and elseif directives take the expression as an argument. The condition expression is the same as that for JavaScript condition expressions, and can also contain server behavior parameters. Directives such as this allow you to choose between alternative code blocks based on the values of, or relationships between, server behavior parameters.
For example, the following JSP code comes from a Dreamweaver server behavior that uses a conditional code block:
@@rsName@@.close(); <@ if (@@callableName@@ != '') @> @@callableName@@.execute(); @@rsName@@ = @@callableName@@.getResultSet();<@ else @> @@rsName@@ = Statement@@rsName@@.executeQuery(); <@ endif @> @@rsName@@_hasData = @@rsName@@.next();
The conditional code block starts with <@ if (@@callableName@@ != '') @> and ends with <@ endif @>. According to the code, if the user enters a value for the @@callableName@@ parameter in the server behavior's Parameter dialog box—in other words, if the @@callableName@@ parameter value is not null, or (@@callableName@@ != '')—then the conditional code block is replaced with the following statements:
@@callableName@@.execute(); @@rsName@@ = @@callableName@@.getResultSet();
Otherwise, the code block is replaced with the following statement:
@@rsName@@ = Statement@@rsName@@.executeQuery();