BackUp LevelNext

Creating Default Variables with CFPARAM

Another way to create a variable is to test for its existence and supply a default value if the variable does not already exist. The following example shows how to use the CFPARAM tag to check for the existence of an optional client variable and to set a default value if the variable does not already exist:

<CFPARAM NAME="Client.FavoriteColor" DEFAULT="Red">

Using CFPARAM

The following shows the syntax of the CFPARAM tag:

<CFPARAM NAME="VariableName" DEFAULT="DefaultValue">

There are two ways to use the CFPARAM tag, depending on how you want the validation test to proceed.

Example: Testing for variables

Using CFPARAM with the NAME variable is a way to clearly define the variables that a page or a custom tag expects to receive before processing can proceed. This can make your code more readable, as well as easier to maintain and to debug.

For example, the following series of CFPARAM tags indicates that this page expects two form variables named StartRow and RowsToFetch:

<CFPARAM NAME="Form.StartRow">
<CFPARAM NAME="Form.RowsToFetch">

If the page with these tags is called without either one of the form variables, an error occurs and the page stops processing.

Example: Setting default values

In this example, CFPARAM is used to see if optional variables exist. If it does, processing continues. If it does not exist, it is created and set to the DEFAULT value.

<CFPARAM NAME="Cookie.SearchString" DEFAULT="temple">

<CFPARAM NAME="Client.Color" DEFAULT="Grey">

<CFPARAM NAME="ShowExtraInfo" DEFAULT="No">

You can also use CFPARAM to set default values for URL and Form variables, instead of using conditional logic.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.