Introduction to ColdFusion Markup Language - The cfif tag
(Page 5 of 7 )
The<cfif>tag is used for evaluating statements. The general syntax looks like this:
<cfif [statement]> ... </cfif>
If the statement in the opening<cfif [statement]>tag evaluates toTrue, then the code between<cfif>and</cfif>is executed. It is important to note that in CFML, "true" (case-insensitive), "yes" (also case-insensitive), and any number not 0, evaluate to a Boolean true. "False," "No," and the number 0 all evaluate to aBoolean false. For example, we can also use<cfif>to check whether variables exist. Consider the following code:
<cfif IsDefined( "Url.date" )>
...
</cfif>
In the preceding snippet, we are using theIsDefined()function to programmatically check whether a variable already exists. If the variable#Url.date#is already defined, then the block of code between the opening and closing<cfif>tag is executed. Otherwise, the block of code will evaluate to False and be skipped entirely. Note thatIsDefined()accepts the name of a variable, not the variable itself, as it's only argument--you do not put pound signs around the variable name, and you must use quotes.
We can also add a<cfelse>tag between the opening and closing<cfif>tags, which then executes a block of code if the statement in the<cfif>evaluates toFalse. Let's look at this in action. Delete the code inindex.cfmfile and enter the following code into the Dreamweaver Document window:
<cfif IsDefined( "Url.date" )>
I would be executed if the variable was defined.
<cfelse>
I would be executed if the variable was NOT defined.
</cfif>
If#Url.date#was defined previous to this code block, the first block between the<cfif>and<cfelse>tag would be executed. If the variable was not defined, then the code between the<cfelse>and</cfif>would be executed instead. In Dreamweaver MX 2004, either test the code in a browser window (press F12) or turn on Live Data view to see the results, as shown in Figure 3-5.

Figure 3-5. The result of running the preceding code. The second code block is executed, because the Url.date variable was not defined.
We can also add one or more<cfelseif>tagsthese are placed between the<cfif>and<cfelse>tags. This allows us to chain together a series ofif statements together. Take a look at this example (seecfif.cfmin the books downloadable code, which is available from the Downloads section of the Apress web site atwww.apress.com):
<cfset thisMonth = 1>
<cfif thisMonth EQ 1>
It is January.
<cfelseif thisMonth EQ 2>
It is February.
<cfelseif thisMonth EQ 3>
It is March.
<cfelse>
It is some other month.
</cfif>
This code tests to see if the value of the#thisMonth#variable equals 1, which in this case evaluates toTrue. Therefore, you will seeIt is January.output when we test the code, as shown in Figure 3-6.

Figure 3-6. In this<cfif>tag, the first block fulfils the if ...condition. Once the expression evaluates to true, the remaining <cfelseif>and <cfelse>tags are skipped.
TIP If you try changing the value of the #thisMonth# variable in the <cfset> tag to 3, you will see It is March. as output. |
Though not a recommended practice unless unavoidable, you can nest if statements inside of each other as well. The following code is exactly like the previous example, only it has a special message when the "thisMonth" variable does not exist.
<cfif not isDefined(thisMonth)>
Month is not defined.
<cfelse>
<cfif thisMonth EQ 1>
It is January.
<cfelseif thisMonth EQ 2>
It is February.
<cfelseif thisMonth EQ 3>
It is March.
<cfelse>
It is some other month.
</cfif>
</cfif>
Next: The cfswitch tag >>
More ColdFusion Articles
More By Apress Publishing
|
This article is 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). Check it out today at your favorite bookstore. Buy this book now.
|
|