Home arrow ColdFusion arrow Page 5 - Introduction to ColdFusion Markup Language

Introduction to ColdFusion Markup Language

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 50
January 19, 2006
  1. · Introduction to ColdFusion Markup Language
  2. · Tags Used in CFML
  3. · The Application.cfm File
  4. · Understanding Common ColdFusion Tags
  5. · The cfif tag
  6. · The cfswitch tag
  7. · The cflocation tag

print this article

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" )>

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.
I would be executed if the variable was NOT defined.

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.
    It is some other month.

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.
<cfif thisMonth EQ 1>
It is January.
<cfelseif thisMonth EQ 2>
It is February.
<cfelseif thisMonth EQ 3>
It is March.
      It is some other month.

blog comments powered by Disqus

- Adobe ColdFusion Just Got More RAD
- How to Access a SQL Anywhere Database with C...
- CFXML: Probing XMLDOM in ColdFusion
- Creating a Web Service with ColdFusion: the ...
- CFAjax: What it is and How to Use it
- Querying SQL 2000 Server from ColdFusion
- Introduction to ColdFusion Markup Language, ...
- Introduction to ColdFusion Markup Language
- Databases and Dreamweaver MX 2004, concluded
- Databases and Dreamweaver MX 2004
- Welcome to Coldfusion MX 6.1, concluded
- Welcome to Coldfusion MX 6.1
- What You Must Know About ColdFusion Flow-Con...
- What You Must Know About Operators in ColdFu...
- Everything You Must Know About ColdFusion Va...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials