How To Build a Server Behavior

Chapter 10 - The Server Behavior Builder
byGareth Downes-Powellet al.
Wrox Press 2003

Building a simple server behavior is usually a pretty straightforward process. Building a very complex server behavior may require a different approach, but the Server Behavior Builder is very often a great step to begin with. After creating your server behavior, you will be able to modify it and make it more complex as you wish.

Here's the process we use to create a server behavior:

  1. Start up the Server Behaviour Builder

  2. Give a name to your server behavior

  3. Create the code blocks and set up the parameters

  4. Tell Dreamweaver where to position the code blocks in the page

  5. Define the interface for the server behavior

If you remember from Chapter 8, where we created a search system, we introduced a little piece of code that gets the value of one variable, and then sets the value of another depending on some conditions. Let's create a server behavior that will do that for us.

Here's the code again:

    <?php       if($HTTP_POST_VARS["price_option"] == "above")       {          $pComp = ">";       }       else       {          $pComp = "<=";       }    ?> 

This bit of code looks at what the user has selected from the drop-down menu for the price range (the possible values are above and below) and then sets the value of $pComp accordingly (either > or <=). In this chapter, we'll build a server behavior that will place that piece of code in our document, but make it flexible so we can change some parts of it.

Starting the Server Behavior Builder

To start the Server Behavior Builder, select the Application panel group and open the Server Behavior tab. Click on the + button, and select the menu entry New Server Behavior...

click to expand

This will pop up the following window:

click to expand

You will notice that the server behavior is dependent on the server model that you're currently working on. Make sure that the Document Type is PHP MySQL. When using the Server Behavior Builder, you must define the correct Document Type because Dreamweaver will only show the Server Behaviors available for that type of document.

This window will also ask you for a name for your server behavior. Let's call it "Set Variable on Form Value". On this interface you can also choose to create your server behavior by copying an already existing server behavior. For now, leave that checkbox unchecked - we'll take a look at it later. Click OK to proceed to the main window of the Server Behavior Builder.

The Code Blocks

The main window of the Server Behavior Builder is where you are going to specify almost all features of our new server behavior. The first thing you should do is to switch on the Advanced button to be able to design our server behavior with the advanced interface. This is because we need to see some of the options that would otherwise be hidden. The window should now look like this:

click to expand

Inserting a Code Block

Click on the + button at the right of the label Code Blocks to Insert: to create a new code block. You will be asked to enter a name for the code block.

click to expand

Dreamweaver will suggest a code block name that is made from your server behavior's name and a block number. It's usually best to go with Dreamweaver's suggestion. Click OK and we're taken back to the main window:

click to expand

The Server Behavior Builder Interface Explained

You can now see that some parts of the interface that were previously grayed out are now available for us to use. Let's have a look at what they do before we continue with our example.

Code Block List

A code block is a piece of code that you want to be inserted by the server behavior into the document. A server behavior can be made of multiple code blocks, because you might want different bits of code to be placed at different locations in the document. In our example, however, we'll only be using one code block

The code block list displays all of the code blocks present in your server behavior. Clicking on a code block enables us to edit its parameters and where it will be positioned in the document:

click to expand

Code Block Area

This is where we can input the actual code for the code block:

click to expand

As you can see, all we have to do is replace the text with our own code.

Code Behavior

This section allows us to determine how to position the code block in the document, and add parameters to the block.

click to expand

We'll look at the Insert Parameter in Code Block... button in detail shortly.

Below the button are drop-down menus that allow us to specify where the code block will be inserted in the document. The first menu gives us the following options:

  • Above the <html> tag

  • Below the <html> tag

  • Relative to a specific tag

  • Relative to the selection

Depending on which option you select, Dreamweaver will ask you to specify a relative position. This means, for example, that if we select the Above the <html> tag option, we will specify in the second drop-down menu that we want our code block to be placed at the beginning of the file, just before the recordsets, just after the recordsets, just above the <html> tag, or in a custom position.

Server Behavior Title and Code Block Selection

The server behavior title and Selection element are only shown when you are in advanced mode.

click to expand

They will let you specify the title of your server behavior, which is what will be displayed in the server behavior list.

Creating a Parameter for Your Server Behavior

The Insert Parameter in Code Block... button is fairly self-explanatory: it inserts a parameter into our code block! A parameter in the code block is simply a placeholder for a value that can vary each time the server behavior is applied to the document. In our example, we are trying to set the value of a variable value depending on the value of the price_option variable. Using a parameter instead of having the word price_option hard coded in the code block allows us to specify the variable we want to check for each time we use the server behavior on your document. In the code block, a parameter is represented by its name enclosed with the characters @@. This will be replaced by the value that you've given the parameter when the server behavior is applied to the document.

The power of a server behavior resides in its ability to have one or more parameters that are inserted into the PHP code. It's a very rare occurrence to have a server behavior with no parameters; a server behavior without parameters would probably be better as a snippet (refer back to Chapter 9 for more information about snippets).

To create a parameter in your code block, click on the Insert Parameter in Code Block... button, which will bring up the following window:

click to expand

The term 'information' in the window is not really accurate, as the information that is inserted is a piece of valid PHP code.

Let's look at our example code again:

    <?php       if($HTTP_POST_VARS["price_option"] == "above")       {          $pComp = ">";       }       else       {          $pComp = "<=";       }    ?> 

To make this code flexible, there are several elements that we'll replace with parameters:

  • The form variable, which is $HTTP_POST_VARS["price_option"]

  • The value which is to be compared against the form variable, which is above

  • The name of the variable that we want to contain the result, $pComp

  • The values that this variable will take if the test is true, >

  • The other value the variable can take if the test is false, <=

Click on Cancel to go back to the main Server Behavior Builder window, so that we can continue building our server behavior.

Continuing the Server Behavior Example

Now that we understand the Server Behavior Builder interface, let's continue with our example.

The Code Block

Select the first code block in the Code Block list, Set Variable on Form_block1, and enter the following code in the code content area:

    <?php       if (@@Form Variable@@ == "@@Form Value@@")       {           $@@New Variable Name@@ = "@@If Value@@";       }       else       {          $@@New Variable Name@@ = "@@Else Value@@";       }    ?> 

This will create our code block. As you can see, we've used five different parameters to replace certain elements of the original code:

  • @@Form Variable@@

  • @@Form Value@@

  • @@New Variable Name@@

  • @@If Value@@

  • @@Else Value@@

Important 

A parameter in the code block is identified by the enclosing @@ characters.

The Server Behavior Builder interface should now look like this:

click to expand

As you can see in the Parameters column, the server behavior builder has automatically noticed that we've added parameters to the code, even though we didn't use the Insert Parameter in Code Block... button. As long as they are enclosed by @@...@@ in the code, the Server Behavior Builder will recognize them as valid parameter names.

Code Block Positioning

Now, we still have to tell the Server Behavior Builder where to insert our code block into the document. As we want to be able to use the new variable in the recordset code, we will select as the position Above the <html> tag (because the recordsets are always positioned before the <html> tag in the PHP page) with the relative position Just before the recordsets.

Check the Identifier option. We want this option to be checked because we only have one code block in our server behavior. This option will let Dreamweaver know that if it finds that code block in your document, it will add the title of the server behavior to the Server Behavior panel.

Server Behavior Title

To set the server behavior's title, enter the following value into the Server Behavior Title box:

    Set Variable on Form Value (@@New Variable Name@@,@@If Value@@,@@Else Value@@) 

When the title is shown in the server behavior list for your document, it will be displayed with the values of the parameters. You can put in as many parameters as you like, but too many might make the name too long!

The Code Block to Select

For Code Block to Select, choose the only code block, which is Set Variable on Form_block1.

The complete Server Behavior Builder interface should now look something like this:

click to expand

We've now finished building most of the server behavior. Click Next... to proceed to the final step: building the interface.

Building an Interface for Our Server Behavior

The interface of our server behavior will be displayed when the user of our server behavior uses it on their document. Dreamweaver provides an easy way for us to generate a user interface: the Generate Behavior Dialog Box, which looks like this:

click to expand

You will notice that the SBB presents you with all of the parameters that we created in the code block. If we had more than one code block, each with its own parameters, they would all appear in this list.

Click on the first item in the list, the Form Variable parameter, and click on the down arrow button that appears. You will be presented with the different types of interface controls that you can use.

Interface Controls

There are many different interface controls that you can use in your server behaviors to allow the user to enter parameters.

Recordset Menu and Recordset Field Menu

These two controls let the user of your server behavior choose values for the parameters from the recordsets defined for the document. Recordset Menus display a list of all of the recordsets defined for the site, while Recordset Field Menus will automatically contain a list of the fields contained in the recordset selected in the Recordset Menu. If you use the Recordset Field Menu without a Recordset Menu control, it will not be populated correctly and Dreamweaver will generate an error when the user tries to launch the server behavior interface.

For example, if we had a behavior where we wanted the user of the behavior to be able to select fields from the rssearch recordset that we used in Chapter 8, we could present them with an interface like this:

click to expand

The top menu is a Recordset Menu and the second menu is a Recordset Field menu.

Editable Recordset Menu and Editable Recordset Field Menu

These two controls work the same way as Recordset Menus and Recordset Field Menus, but the user can edit the values that they've chosen.

CF Data Source Menu

The ColdFusion Data Source Menu provides the user with a menu populated with items from a ColdFusion Data Source. There's no reason to use it when designing a PHP server behavior.

Connection Menu, Connection Table Menu, Connection Column Menu

Connection Menus, Connection Table Menus, and Connection Column Menus are populated with the Connection data for the site. This is a convenient way to present the user with the different values from a Connection. They work in a similar way to Recordset Menus, in that they automatically populate depending upon user choice.

Dreamweaver will generate an error if you try to use one of them without having the parent UI control in your interface. For example, you cannot add a Connection Column Menu if you have no Connection Table Menu, and you cannot have a Connection Table Menu if you have no Connection Menu.

The following picture shows you an example of these three elements in action:

click to expand

Text Field

A Text Field is the common text box. Text-Fields are useful when you want the server behavior's user to be able to enter anything that they like as a parameter.

Dynamic Text Field

Dynamic Text Fields work in almost the same way as Text Fields, but add a little button that will invoke a DataSource dialog where the user can select a value from the dynamic data defined for the document.

You may think that if it works the same way as a Text Field but adds a feature, why not always use it instead of a Text Field? Well, if the user has an icon telling them that they can use dynamic data, they will be tempted to use it, which is not always what you want. For example, in our server behavior, in the case of the Form Value parameter, we don't want the user to enter a dynamic variable because we enclosed the parameter Form Value with quotes in our code block. If the user entered dynamic data as a parameter, it wouldn't work correctly.

URL Text Field

A URL Text Field is a control that allows the user to specify a URL as one of the parameters. The user will be able to browse to find a file that they want the server behavior to interact with.

Here's what the user is presented with:

click to expand

Numeric Text Field

The Numeric Text Field will allow you to get a smaller Text Field that requires a numeric entry from the user.

Unfortunately, however, you will have to edit the .htm file generated by the Server Behavior Builder in order to get this functionality working. We will not cover the modification of this file, as it would go beyond the scope of this book.

Recordset Fields Ordered List

As for the Numeric Text Field, this UI control requires some modification on the generated files. We will therefore not cover it here.

Text Field Comma Separated List

A Text Field Comma Separated List presents the user with an input area where they can enter multiple values. When passed to the server behavior, these values are concatenated together as a comma-separated list.

List Menu, Checkbox and Radio Group

The List Menu, Checkbox, and Radio Group are familiar ways for the user to select options from your interface.

Here's some radio buttons:

click to expand

The value of a checkbox or radio button is either nothing if it is unchecked or true if checked. Checkboxes and radio buttons are mainly used with conditional code blocks, which are code blocks that are inserted by the server behavior into the document only if a condition is true.

The use of conditional code blocks is outside the scope of this book, but there's more information about it in the Dreamweaver documentation.

The Interface of Our Example

Getting back to our example server behavior, we have the following parameters:

  • Form Variable

  • Form Value

  • New Variable Name

  • If Value

  • Else Value

We will use the Text Field UI control for each of our parameters, except the first, Form Variable, which will be a Dynamic Text Field instead. This is because the value for Form Variable is defined in the Bindings panel, and therefore can be retrieved with a Dynamic Text Field, which is much easier and less error-prone than trying to type something like $HTTP_POST_VARS["price_option"] into a textbox!

Your Generate Behavior Dialog Box should now look like this:

click to expand

Finishing the Server Behavior

Once you have all your Interface controls set up, click on OK to finalize your server behavior.

If you click on the + button in the Server Behavior panel, you will see your new server behavior menu item.

click to expand

Click on the Set Variable on the Form Value menu item to see what your server behavior's interface looks like:

click to expand

Let's fill it in with the values that we used for the search engine in Chapter 8:

  • Form Variable: $HTTP_POST_VARS["bed_option"]

  • Form Value: less

  • New Variable Name: pComp

  • If Value: >

  • Else Value: <=

When you click on OK the server behavior will generate the following code, placed above the recordsets definition in the document:

    <?php       if($HTTP_POST_VARS["price_option"] == "above")       {         $pComp = ">";      }      else      {         $pComp = "<=";      }    ?> 

How To Copy a Server Behavior

It's tempting to try and design one server behavior that can handle all the tasks that you throw at it. Don't fall into the easy trap, which is to make your server behavior more and more complex, just to be able to handle every case you may encounter. Instead, I would recommend that you design multiple server behaviors, and group them with a common prefix name. An easy way to produce similar behaviors based on a common one is to copy an existing behavior and modify it.

In the example we built, we used the following code:

    <?php       if(@@Form Variable@@ == "@@Form Value@@")       {          $@@New Variable Name@@ = "@@If Value@@";       }       else       {          $@@New Variable Name@@ = "@@Else Value@@";       }    ?> 

and that forced us to use a Text Field control for the Form Value parameter. We could have removed the quotes surrounding the parameter and been able to use a Dynamic Text Field instead.

But we can also create a new Server Behavior that will do that specifically for us. To copy a server behavior, click on New Server Behavior... in the Server Behavior panel in the Application panel group.

Enter the name of the new server behavior, and check the option to copy an existing server behavior. Now select the Set Variable on Form value behavior that we want to copy. The rest of the operation is exactly the same as for a brand-new server behavior, so just modify the code block.

    <?php       if(@@Form Variable@@ == "@@Form Value@@")       {          $@@New Variable Name@@ = "@@If Value@@";       }       else       {          $@@New Variable Name@@ = "@@Else Value@@";       }    ?> 

Give the new behavior a new name, and we're done!



Dreamweaver MX PHP Web Development
Dreamweaver Mx: Advanced Php Web Development
ISBN: 1904151191
EAN: 2147483647
Year: 2001
Pages: 88

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net