This article, the second of two parts, explains how to use ColdFusion 6.1 in conjunction with Dreamweaver MX 2004 to build dynamic web sites. It is excerpted from chapter one of the book ColdFusion Web Development with Dreamweaver MX 2004, written by Jen and Peter deHaan et al. (Apress, 2004; ISBN: 1590592379).
Welcome to Coldfusion MX 6.1, concluded - Getting Started with CFML (Page 5 of 5 )
Setting a Variable
To set a variable in a page, type
<cfset myVar = "myValue">
To output this variable, type
If you save this code in a.cfmfile and view it in a web browser, the text “myValue” will be output to the screen.
When you output variables in ColdFusion MX 6.1, remember that they must be enclosed in pound signs (#). Without pound signs, ColdFusion MX 6.1 assumes that you want to output a string, so typing<cfoutput>myVar</cfoutput>would simply output myVar to the window. Likewise, if we forget the<cfoutput>tags, ColdFusion MX 6.1 won’t parse the variables, and #myVar# will show up as the output! One place where ColdFusion MX 6.1 differs from other programming languages is that it doesn’t require you to declare a data type for a variable before setting a value. ColdFusion MX 6.1 automatically converts variables and determines their data type for you.
ColdFusion MX 6.1 also has a very extensive list of functions available, including the ability to create your own user-defined functions (UDFs). For example, you could simply add<cfoutput>#Now( )#</cfoutput>to a web page to have it display the current server date and time on the screen.#Now( )#is a built-in ColdFusion MX 6.1 function that takes no arguments and returns the current date and time of the server. Several other date and time functions allow you to extract just the year, month, hour, second, or any component of a date, along with numerous other formatting options to choose for displaying dates.
Looping with <cfloop>
This tag has a few different parameters and performs different tasks depending on which attributes you specify, but one of the easiest ways to call this tag is like this:
<cfloop from="1" to="3" index="i"> ... </cfloop>
This example loops from 1 to 3. This is similar to afor...nextloop structure in other languages. Using other attributes of the<cfloop>tag,do...whileand other types of loops can be used. For each time you go through the loop, you set a variable called“i”that holds the current value. If you changed the code to:
ColdFusion MX 6.1 allows you to send e-mails quickly and easily by using the <cfmail> tag, as shown in the following snippet:
<cfmail from = "firstname.lastname@example.org" to = "email@example.com" subject = "your subject here"> this is the body of the e-mail. </cfmail>
As long as you have entered a valid mail server in the ColdFusion MX 6.1 Administrator, you will be able to send e-mails from ColdFusion MX 6.1. You could easily modify this example to allow you to send e-mails to a variable that a user typed into a web form; this would enable you to build a simple form that allows visitors to e-mail questions and feedback to the webmaster.
In this chapter, we described many different areas of the ColdFusion MX 6.1 environment, such as the CFML language, ColdFusion MX 6.1 application server, and how these interact with Dreamweaver MX 2004. We covered:
The background and history of ColdFusion and how it integrates with the Web today
The advantages and disadvantages of ColdFusion MX 6.1
How to install and set up the standalone version of ColdFusion MX 6.1 server on the Windows platform
How to set up a ColdFusion MX 6.1 web site in Dreamweaver MX 2004
How to get started with CFML
The next chapter focuses on using databases and SQL, which is used to select, insert, update, and delete data from databases. We will also take a look at how to create a data source in the ColdFusion MX 6.1 Administrator, which allows ColdFusion MX 6.1 to talk to databases.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.