ColdFusion
  Home arrow ColdFusion arrow Page 6 - 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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 9
    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 cfswitch tag


    (Page 6 of 7 )

    ColdFusion also has another conditional logic mechanism that is usually more efficient and results in more modular and easy-to-read code: the<cfswitch>tag. We could change the series ofif ...statements in the first "thisMonth" <cfif> example into the followingswitchstatement (seecfswitch.cfmin the books downloadable code):

    <cfset thisMonth = 1>
    <cfswitch expression="#thisMonth#">
      <cfcase value="1">
       
    It is January.
      </cfcase>
      <cfcase value="2">
       
    It is February.
      </cfcase>
      <cfcase value="3">
       
    It is March.
      </cfcase>
      <cfdefaultcase>
       
    It is some other month.
      </cfdefaultcase>
    </cfswitch>

    If you replace the code from the previous example with this, then you will also seeIt is January.in the browser window.

    The expression in the<cfswitch>tag is executed only once, and ColdFusion tries to match the expression to a case. If ColdFusion finds no cases that match, it executes the optional<cfdefaultcase>block if it is present. The<cfdefaultcase>acts as the<cfelse>statement did previously. Once ColdFusion finds a matching<cfcase>statement, it skips over the remaining<cfcase>and<cfdefaultcase>blocks. The case statement also accepts a list of comma-separated values, which allows us to say "If the value is X, Y, or Z, then do this."

    Here's an example of this (defaultcase.cfm):

    <cfset thisMonth = 1>
    <cfswitch expression="#thisMonth#">
      <cfcase value="1,2,3">
        It is January, February or March.

      </cfcase>
      <cfcase value="4,5,6">
       
    It is April, May or June.
      </cfcase>
      <cfdefaultcase>
       
    It is something else.
      </cfdefaultcase>
    </cfswitch>

    The preceding code block does the following: if#thisMonth#is 1, 2, or 3, then the first case will match and the code block will be executed. Otherwise, the<cfswitch>keeps trying to match the value of#thisMonth#to the values listed within the<cfcase>tags until it finds a match or encounters the<cfdefaultcase>tag.

    There is one important difference between using a series of<cfif>statements and a<cfswitch>statement. We can test for multiple expressions by using a<cfif>statement by separating them with anANDorORkeyword.

    <cfif (IsDefined("Url.Name")) AND (Url.Name EQ "Larry")>
      ...
    <cfelseif (IsDefined("Url.Number")) AND (Url.Number EQ 7)>
      ...
    </cfif>

    This behavior isn't possible using<cfswitch, which can evaluate only a single expression and match a value or series of values, so it isn't always possible to rewrite a<cfif>block as a<cfswitch>. You can also see that, because you are comparing the value ofUrl.Nameto a string, you do need to enclose the string in a pair of quotes.

    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

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT