ColdFusion
  Home arrow ColdFusion arrow Page 5 - Introduction to ColdFusion Markup Language
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
COLDFUSION

Introduction to ColdFusion Markup Language
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-01-19

    Table of Contents:
  • Introduction to ColdFusion Markup Language
  • Tags Used in CFML
  • The Application.cfm File
  • Understanding Common ColdFusion Tags
  • The cfif tag
  • The cfswitch tag
  • The cflocation tag

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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>

    More ColdFusion Articles
    More By Apress Publishing


       · This article is an excerpt from the book "ColdFusion Web Development with...
     

    Buy this book now. 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.

    COLDFUSION ARTICLES

    - 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...
    - My First Application on ColdFusion MX Server






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT