If you want to get a good start on learning ColdFusion, look no further. This article covers a whole bunch of CFML tags and some basic functions. It is the first of two parts, and excerpted from chapter three of the book ColdFusion Web Development with Dreamweaver MX 2004, written by Jen and Peter deHaan et al. (Apress; ISBN: 1590592379).
Introduction to ColdFusion Markup Language - Understanding Common ColdFusion Tags (Page 4 of 7 )
In this section we look at some more common CFML tags that you'll use in everyday ColdFusion development. The functions these tags have range from setting variables and interacting with the operating system on the server, to sending e-mails to users or administrators.
The<cfparam>tag is similar to the<cfset>tag used in the previous section, but with one important difference:<cfparam>will set the variable only if it does not yet exist. This is useful when you want to set default values or need to make sure that a variable exists before trying to use it.
The syntax is as follows:
<cfparam name="Url.date" default="#Now()#">
What this snippet does is check to see if a URL variable named "date" exists; if it does, then<cfparam>does nothing. If the variable is not found, then it is created and given a value equal to the current date and time. This way, you are not overwriting variables if they are already defined, and you can always be sure that variables exist and have values. Note that if the value of the "name" attribute (the variable name) does not have a prefix, the server will assume you are setting avariable in the local "variables" scope.
With the simple addition of a type attribute, this tag can also check to make sure that a variable is of a certain data type. Here's an example:
In this case, if the value of the#Url.date#variable is not a valid date, ColdFusion will throw an error, as shown in Figure 3-2.
Figure 3-2. An error is thrown if the data type specified in the<cfparam>type attribute is not the same as the data type of the variable being set.
We cover error handling in more detail in Chapter 8.
If you provide only a name and type attribute for the<cfparam>tag, ColdFusion makes sure that the variable already exists and is of the correct data type. If either of these conditions fail, ColdFusion will throw an error.
We can view the supported types in Dreamweaver MX 2004. Click on the CFML Basic tab on the Insert bar in Dreamweaver, then click on the cfparam button, which looks like an exclamation mark and is ninth from the left, as shown in Figure 3-3.
Figure 3-3. The Dreamweaver MX 2004 Insert bar, with the basic CFML functions shown and the cfparam button illustrated
When you click the cfparam button, you are presented with the dialog box in Figure 3-4, in which you can set the attributes we looked at previously.
Figure 3-4. The Dreamweaver MX 2004 cfparam Tag Editor