Home arrow ColdFusion arrow Page 3 - 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 Application.cfm File
(Page 3 of 7 )

The first file is calledApplication.cfm.If it exists, it is called before any other ColdFusion code is executed. If we request a file calledindex.cfm, ColdFusion first checks for a file calledApplication.cfmin the same directory as the requestedindex.cfmfile. IfApplication.cfmis not found, ColdFusion checks the current directory's parent directory for the file. This process continues until a file calledApplication.cfmis found or there are no more parent directories to check. If noApplication.cfmfile is found, ColdFusion simply continues processing the requested document.

If anApplication.cfmis found, ColdFusion processes this file and then goes on to process the requested file, which in our example wasindex.cfm. You can think of it as an implicit include file--an "on request start" if you will. TheApplication.cfmfile is used as a container for global content that developers want to be available to every page, such as site-wide variables (like data source names) and for code that should be executed on every page request, although it is possible to have manyApplication.cfmfiles within a single web site.

A similar file calledOnRequestEnd.cfmexecutes after theApplication.cfmand the requested file have finished executing, but it executes only if anApplication.cfmfile exists inside the same folder. TheOnRequestEnd.cfmfile is useful for controlling debugging output that would be sent to the screen or for writing a footer to the screen.

It is important to remember the proper spelling and casing of these file names:Application.cfmandOnRequestEnd.cfm. Although Windows environments are not case-sensitive and will treatapplication.cfmandApplication.cfmas the same file, Linux and UNIX environments are case-sensitive, and will treat the two files differently. Make sure you name them exactly as we did previously, so that if you migrate from a Windows environment to a Linux/Unix environment in the future, you don't have to worry about renaming several files.

Now let's look atApplication.cfmin action to illustrate how and in which order the files are processed.

  1. Open the cfbook site again in Dreamweaver MX 2004. Let's begin by creating a simpleApplication.cfmfile in the cfbook folder. Create the new file and enter the following ColdFusion code:

    <cfset numCats = 2> 
  2. Now create another new file calledindex.cfmin the cfbook folder and enter this code:

  3. Openindex.cfmin a web browser by choosing File->Preview in Browser and selecting your default browser (in Dreamweaver MX 2004). When viewing ColdFusion pages in your web browser, it is important that the file is placed within the Web-root and that you type in the URL to the file (such ashttp://localhost:8500/index.cfm), not the physical path (C:\CFusionMX\ wwwroot\index.cfm, for example). Otherwise, you will receive unexpected results, because the code will not be put through to the ColdFusion server. When the browser opens, you will simply see the number 2 in the window. This works becauseApplication.cfmis being executed before theindex.cfmpage. In fact, the#numCats#variable should be available to all pages in the same directory, and any other child directories, as long as no otherApplication.cfmfiles are found in the directory tree.

    Once ColdFusion locates anApplication.cfmfile, it stops looking for otherApplication.cfmfiles. If we have a subfolder under the cfbook folder that also contains anApplication.cfmfile, any files within that subfolder will not be able to access variables or functions defined in the/cfbook/Application.cfmfile.
  4. Let's create another variable in theApplication.cfmfile. Add another line, as follows:

    <cfset numCats = 2>
    <cfset Today = "Saturday">
  5.  Because the value of#numCats#is numeric, you do not have to enclose it in quotes while setting it. However, when working with strings, it is very important to make sure that they are enclosed in either single quotes or double quotes as shown in the<cfset>tag. Without the quotes around "Saturday" in the second <cfset>, ColdFusion will assume you are setting the "Today" variable equal to the value of a "Saturday" variable and will throw an error if said variable does not exist. In the case of numeric values, ColdFusion simply uses the value with or without quotes (this makes sense, because CFML variable names cannot begin with a numeric character). We can also set variables equal to the result of a function. ColdFusion MX 6.1 has hundreds of built-in functions available to developers, and we cover several throughout this chapter. One useful function is the#Now()#function covered briefly in Chapter 1. This function does not take any parameters and simply returns the current date/time of the server.

    Back inindex.cfm, delete the existing code, type in the following and save it:

    <cfset datetime = Now()> <cfoutput>#datetime#</cfoutput>

Because you aren't outputting the value of#Now()#to the screen and it is used within a tag and not inside quotes, it is not necessary to enclose the function in pound/hash signs while setting it. A good rule of thumb is that any time you are not inside quotes and are inside of function parentheses or a tag (not between opening and closing tags, but inside a tag), you do not need the hash marks. If you view this page in a web browser, you should see an output similar to Figure 3-1.

Figure 3-1.   Outputting the value of a variable with<cfoutput>

Although this isn't the most user-friendly date/time format, ColdFusion does offer some excellent formatting options. Two useful formatting functions are#DateFormat()#and#TimeFormat()#. Both functions take in a date as the first parameter and an optional formatting mask. Change theindex.cfmcode to the following:

<cfset currDateTime = Now()> <cfoutput>#DateFormat(currDateTime)#</cfoutput>

If you test this code, it yields an output similar to the following in the browser window, depending on the date the code is executed:


We cover the formatting options of#DateFormat()#and#TimeFormat()#in Chapter 4. Now let's look at some more of the common CFML tags that you will encounter on a regular basis.

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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials