ColdFusion MX: From Static to Dynamic in 10 Steps

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps

By Barry Moore

Table of Contents
Step 1.  The Basics

As alluded to earlier, the two major components of CFML are tags and functions. In this section, you will be introduced to both aspects of CFML.

ColdFusion Tags

CFML tags are the building blocks of ColdFusion applications. ColdFusion tags are used for performing operations and are very similar in use and syntax to HTML tags. This makes CFML very quick and easy to learn. ColdFusion currently supports more than 80 tags that enable you to do everything from querying databases and sending email to authenticating users and running stored procedures.

Like HTML tags, CFML tags are enclosed in angle brackets (< >) and might have required and/or optional attributes. Also like HTML tags, CFML tags might be either container tags or empty tags. Empty tags simply have a single tag (as illustrated in the following code) while container tags have an opening and closing tag associated with them.

<CFSET FirstName="Barry"> <CFOUTPUT>Welcome back #FirstName#</CFOUTPUT>

In the preceding example we are using an empty tag, <CFSET>, to set the value of a variable called FirstName equal to the value of Barry. (You'll learn more about variables in Step 2, "Using Variables.") Then we use a container tag, <CFOUTPUT>, to print out the words Welcome back and the value of the FirstName variable, in this case Barry. The output to a web browser therefore would be Welcome back Barry.

"If CFML and HTML are so similar, how can you tell them apart?" I can hear you asking this already. The answer is quite simple: All CFML tags begin with <CF... >. This is the indicator for the ColdFusion Server to perform some sort of processing on the tag or its contents.

These many tags work together and fall into broad categories, such as the following:

  • Application framework

  • Database manipulation

  • Data output

  • External interfaces

  • File manipulation

  • Flow control

  • Forms

  • Internet protocols

  • Security

  • Utilities

  • Searching

If you have a need that is not met by an existing tag, ColdFusion gives you the capability to create and reuse your own custom tags. Many developers write custom tags and release these tags into the public domain for others to use. One location to search for such custom tags is the Macromedia ColdFusion Developers Exchange on the Macromedia web site at www.macromedia.com.

ColdFusion Functions

Functions are encapsulated bits of code that perform a certain task and provide a result. Like ColdFusion tags, ColdFusion functions are embedded into web documents and are used to return or manipulate data, such as formatting a date or replacing characters in a string. Functions usually need some sort of input data to do their job. This input is commonly referred to as a parameter or an argument and is placed inside the parentheses of the function, as illustrated in the following example. Different functions require different arguments; some require several arguments, some only one, and some none at all.

The basic syntax works like this:

FunctionName(Argument)

An example would look like this:

IsLeapYear(2003)

The preceding IsLeapYear() function checks the argument providedin this case, the year 2003to see if it is a leap year and provides either YES or NO as a result.

ColdFusion currently supports more than 250 different functions in 15 categories. Some of the categories are as follows:

  • Array functions

  • Authentication functions

  • Conversion functions

  • Date and time functions

  • Decision functions

  • Display and formatting functions

  • Dynamic evaluation functions

  • International functions

  • List functions

  • Mathematical functions

  • Query functions

  • String functions

  • Structure functions

  • System functions

  • Other functions

Functions must be used either within a ColdFusion tag itself or between container tags, such as <CFOUTPUT>, as illustrated in the following:

<CFSET Answer = Sqr(49) >

or

<CFOUTPUT>The square root of 49 is #Sqr(49)#</CFOUTPUT>

The first tag in this example uses the Sqr() function to set a variable called Answer equal to the value of the square root of 49. In the second example, we would output the phrase The square root of 49 is 7 to our browser. You might be wondering about those # characters in the <CFOUTPUT> line. These characters have special significance to ColdFusion and will be explained further in "The <CFOUTPUT> Tag" section later in this step. You can also find more information in the sidebar titled "Give Your ColdFusion Templates a Good Pounding." For now, let's press onward. As with ColdFusion tags, if no existing function provides a particular functionality that you commonly use, ColdFusion gives you the ability to create your own user-defined functions. Some developers create user-defined functions and share them as open source with the rest of the ColdFusion community. You can find many of these functions at www.cflib.org.

See the reference section on this book's web site (www.LearnColdFusionMX.com) for a complete listing of ColdFusion tags and functions.


    Team-Fly    
    Top
     

    Категории