In this chapter you will write your first Cold fusion template and learn the basic ColdFusion code that will prepare you for the rest of this book. Once you get started coding your first template should take you less than 15 minutes. Good luck and welcome to Cold fusion
The first thing you need to do is ensure that the following programs are installed on your computer:
• A Web server (for example WebSite II? Apache PWS etc.)
• ColdFusion Application Server 4.5
• ColdFusion Studio 4.5
You need to install these programs to complete the exercises in this book. If you have been through the installation procedure described in the Introduction continue to the next section. Otherwise please refer to Appendix A “Installing ColdFusion” and follow the directions for installation. If you believe that you may already have these programs installed Appendix A can also help you verify the installation
Now that you have successfully installed a Web server ColdFusion Application Server and ColdFusion Studio you are ready to code your first ColdFusion template.
Understanding CFML vs HTML
Coding ColdFusion Markup Language (CFML) is very similar to coding Hypertext Markup Language (HTML). CFML and HTML are both tag-based languages that usually have beginning and ending tags. CFML has been designed to work seamlessly with HTML. CFML is used to determine what content will appear on the page
It is important that you have a clear understanding of HTML and how it works before you continue with this book. If you neid to learn more about HTML. refer to the book recommendations in the” Requirements” section of the Introduction.
When coding a CFML template, you will useHTML and CFML together. So a basic dTML page will become a ColdFusion template if CFML is added to the code Here is a basic HTML tag:
Now compare this to a basic CFML tag:
As you can see they look very similar. Of course they perform different tasks and that is exactly what this book will teach you
Using Cold Fusion Studio
Coldfusion templates and HTML pages are both text-based documents that can be edited in any text editor. Throughout this book, you will edit all your templates using ColdFusion Studio Allaire’s Web editor.
Allaire’s Cold Fusion Studio started out as an enhanced HTML editor called Homesite created by Nick Bradbury. In March 1997 Allaire acquired Homesite decided to expand on Homesite’s features and created a new product: Cold Fusion Studio. ColdFusion Studio is a full-fledged Web-development editing application that integrates seamlessly with ColdFusion Application Server. It enables you to track projects add version control preview Web pages edit in WYSIWYG \What-You-See-Is-What-You-Get) mode and much more. To learn more about Studio 4.5 refer to Chapter 20 “Using ColdFusion Studio
To open: COldFusion Studio click the Windows Start button and go to Programs :> Cold Fusion Studio 4.5 (Program Group) :> ColdFusion Studio 4.5. You can also click the Windows Start button and select Run. Then, in the Open field, type C:\Program Files\ Allaire \ColdFusion Studio4 vcfstudiod.exe or the path that corresponds to the location in whi -h you’ve installed CF Studio
You will notice that when you open Studio (see Figure 1.1) an HTML page is already provided with all the necessary head and body tags. This is the page that you will use to code your first ColdFusion template. You will also notice that many toolbars and windows are in the Studio interface. These will be explained as needed throughout the book, but if you would like more information now on these toolbars and windows you can refer to Chapter 20.
Saving a File
Before you begin coding, follow these steps to save your file to a new directory:
Go to File > Save on the CF Studio menu bar
Make sure that the Look In pull-down menu displays the Web server root directory.
Name this folder sybex and then open it. Name this file home.cfrn and click the Save button.
You have given this file a CFM extension so that the Coldfusion Application Server will recognize it and process all the Cold Fusion tags that you will eventually add to it. For a More detailed descriptio” of how ColdFusion templates arc processed please refer to “How Does ColdFusion Work” in the Introduction
You are now going to add some text and ColdFusion code to your new home. cfm file. At this point, just follow the example. The code will be explained in the next section. Add the code in Listing 1.1 to your new home. cfm file.
<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HT\lL 4.0 Transitional//EN’>
<TITLE>My Home Page</TITLE>
<HR><B>Welcome to My Home Page!</B><HR>
<B>Today’s date ‘;s:</B><BR>
<CFSET today – Ncw(»
Wasn’t that easy? You have just created your first Cold Fusion template! Let’s take a closer look at what vou did.
In the preceding example ColdFusion studio provided you with the standard HTML page codes to create a basic HTML page. You then added text and included todays date.
Because basic HTML does not provide a way to display dynamic (or changing) information such as the current date you added Cold Fusion code to display the date. ColdFusion tags and HTML tags are used in a similar way. Both CFML and HTML usually have a beginning and end tag that encloses some information Each set of beginning and end tags always has the same name, except the end tag name is preceded by a slash (I). In this case, you used one of the most basic of all ColdFusion tags, CFOUTPUT. This is cne of the tags you can use when you want to display ColdFusion data. The CFOUTPUT tag tells the ColdFusion Application Server that it needs to evaluate all the ColdFusion variables between the beginning and end CFOUTPUT tags and then display it. The format for using a CFOUTPUT tag is written as the following:
Using CFSET to Create a Variable
Between your CFOUTPUT tags you created a Cold Fusion variable called today using the CFSET tag. CFSET is used to create and modify variables. The syntax for the CFSET tag is written as the following
<CFSET your_variable_name – the_value_of_your_variable>
To understand variables think of the stock market. Every stock on the stock market has a name. For example, Allaire’s stock has the name ALLR. Now almost every time you look at the value of the ALLR stock it will have changed. So the name of the stock is the same, but its value changes. In the same way variables are given names, but their values can change. For simple you could create a variable named AllR with an initial value of 120. Over time, the valu~H the variable can change to 140, for example, without . the name changing. At any time, the current value can be accessed through the variable name AllR …
In the previous example you created the variable today and assigned it the value of Now(). The value Now() is actually a Coldlusion function that returns the current timestamp (or the date and time) that is recorded on the server (functions are discussed in
more detail in the next section). If you were to reload your Web page right now you would see a new timestamp returned because the value of the variable today has changed since you last loaded the home. cfm page
Several rules about varieties are useful to know when coding your templates:
• ColdFusion variables that will be displayed in the browser usuallv need to be enclosed within pound (#) signs-but not always. For example, you did not have to enclose your today variable within # signs to set it, but you did have to enclose it within # signs to display it.” Chapter 4, “Creating and Manipulating Variables,” provides more information about when you should and should not use # signs with your variables. For now, when in doubt, use # signs.
• When you want to display a Coldfusion variable it needs to be inside a CFOUTPUT tag or other similar tag (which you will learn about later in this book).
• ColdFusion variables cannot contain reserved words spaces special characters. or begirt with a number