Introduction to ColdFusion Markup Language

Team-Fly    

Macromedia® DreamWeaver® MX Unleashed
By Matthew Pizzi, Zak Ruvalcaba
Table of Contents
Chapter E.  ColdFusion MX Tags and Beyond


Most ColdFusion development is done by writing CFML for server-side processing, in conjunction with HTML for page layout and static elements. CFML is a full programming language, with tags to perform common programming tasks, such as the following:

  • Create and manipulate variables.

  • Write self-contained, reusable functions.

  • Evaluate expressions to manipulate variable values.

  • Use built-in functions for common data manipulation.

  • Provide program flow control such as If Then branches.

  • Repeat blocks of code using loops.

Tags

ColdFusion tags are not case sensitive and ignore whitespace, like HTML, but it is good practice (as it is with HTML) to stick to one case it makes the code easier to understand and won't confuse you when you have to write case-sensitive languages such as JavaScript.

Some tags are single tags, like the HTML <img> tag. For example:

 <cfset user = form.username>  

This <cfset> tag sets a variable called user to the value of a form control called username.

Many other tags have opening and closing tags, with a tag body in between, as most HTML tags have. For example:

 <cfoutput> Hello #user#!</cfoutput>  

This <cfoutput> tag writes a message to the page containing the value of the variable called user.

Like HTML, CFML tags can also have attributes that give additional information about the use of the tag. For example

 <cfoutput query="stafflist"> #firstname#</cfoutput>  

This <cfoutput> tag also outputs a variable value, but this variable, fullname, comes from a database query named stafflist.

Examples of Simple CFML Tags in Use

Both Dreamweaver and ColdFusion provide lots of documentation about the use of ColdFusion. Later on in the appendix, you will see how to access information about all the ColdFusion tags available, but this section describes the use of relatively simple ColdFusion tags to demonstrate how to use ColdFusion in practice. Table E.1 lists a few of the common CFML tags.

Table E.1. Simple CFML Tags
CFML Tag Description
<cfset> Used to define a variable and give it a value.
<cfquery>…</cfquery> Used to make a SQL query to a database and return a recordset.
<cfoutput>…</cfoutput> Displays a query, variable value, or other operation in the Web page.
#variablename# Surrounding a variable name or recordset fieldname with hash signs tells ColdFusion to output the value of the variable or field name.
<cfif>…</cfif> Creates a conditional statement, so that one thing will be done if a condition is met, and something else will be done if the condition is not met.
<cfelse> and <cfelseif> Used in conjunction with <cfif> so that a number of different conditions can be tested.
<!--- this is a comment ---> A CFML comment. It looks like an HTML comment, but it has three dashes, not two. CFML comments are not shown in the Web page, so they cannot be seen by the user if they View Source. Only the CFML designer can see them.

The following code listing, Listing E.1, shows these tags being used together. A database is queried, using a connection called conStaff, to retrieve selected fields from a table called tblStaff. The results of the query are listed on the page in a table, with the Managing Director shown in red, and the Finance Director shown in blue.

Listing E.1 Example of Using Simple CFML Tags to Produce a Conditional Output from a Database
 <cfquery name="rsall" datasource="conStaff"> SELECT firstname, lastname, title, phone FROM tblStaff <!--- Selects all staff members from database ---> </cfquery> <html> <head> <title>CFML Example</title> </head> <body> <table width="50%" border="1">   <tr bgcolor="#FFFFCC">       <th>First Name</th>       <th>Last Name</th>       <th>Title</th>       <th>Phone</th>   </tr>   <cfoutput query="rsall">      <tr>          <cfif #rsall.title# is "MD" > <!--- apply special formatting to MD and FD --->               <td><font color="red">#rsall.FirstName#</font></td>               <td><font color="red">#rsall.LastName#</font></td>          <cfelseif #rsall.title# is "FD" >               <td><font color="blue">#rsall.FirstName#</font></td>               <td><font color="blue">#rsall.LastName#</font></td>          <cfelse>               <td>#rsall.FirstName#</td>               <td>#rsall.LastName#</td>          </cfif>          <td>#rsall.Title#</td>          <td>#rsall.Phone#</td>      </tr>   </cfoutput> </table> </body> </html> 

As you can see from the listing, the CFML code is relatively simple and complements HTML very well.


    Team-Fly    
    Top


    Macromedia Dreamweaver MX Unleashed
    Macromedia Dreamweaver MX 2004 Unleashed
    ISBN: 0672326310
    EAN: 2147483647
    Year: 2002
    Pages: 321

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net