Working with Templates


Back in Chapter 4, "Previewing ColdFusion," I walked you through creating several simple applications. ColdFusion applications are made up of one or more files, each with a .cfm extension. These files often are referred to as templates; you'll see the terms templates, files, and even pages used somewhat interchangeably. Just so you know, they all refer to the same thing. I'll explain why the term templates is used in a few moments.

NOTE

As explained in Chapter 3, "Accessing the ColdFusion Administrator," the URL used with ColdFusion will vary based on whether or not an external Web server is being used. For the sake of simplicity, all URLs used in this and future chapters assume that ColdFusion is being used in conjunction with the integrated Web server ("standalone" mode). As such, you'll see the port address :8500 specified in all URLs (both in the content and the figures). If you are not using the integrated Web server simply omit the :8500 from any URLs.


Creating Templates

As already explained, ColdFusion templates are plain text files. As such, they can be created using many different programs. Obviously, a good choice for ColdFusion developers, as already seen, is Macromedia Dreamweaver MX. So that's what you'll use here and throughout the rest of this book.

To create a new ColdFusion fileor template; as I said, the terms are used interchangeablysimply start Dreamweaver MX. The editor will be ready for you to start typing code, and what you save is the ColdFusion file, as long as you save it with a .cfm extension, that is.

NOTE

There are two other file extensions used with ColdFusion, .cfc and .cfr. We'll look at those files in future chapters.


The code shown below is the contents of a simple ColdFusion file named hello1.cfm. Actually, at this point no ColdFusion code exists in the listingit is all straight HTML and text, but we'll change that soon. Launch Dreamweaver MX (if it is not already open), and type the code as shown next (see Listing 8.1).

Listing 8.1. hello1.cfm
 <html> <head>  <title>Hello 1</title> </head> <body> Hello, and welcome to ColdFusion! </body> </html> 

TIP

Tag case is not important, so <BODY> or <body> or <Body> can be usedit's your choice.


Saving Templates

Before ColdFusion can process pages, they must be saved onto the ColdFusion server. If you are developing against a local server, with ColdFusion running on your own computer, you can save the files locally. If you are developing against a remote server, you must save your code on that server.

Where you save your code is extremely important. The URL used to access the page is based on where files are saved, and how directories and paths are configured on the server.

As explained back in Chapter 2, all the files you create throughout this book will go in directories beneath the ows directory under the Web root. To save the code you just typed, create a new directory named 8 under ows and then save the code as hello1.cfm. To save the file, do one of the following:

  • Select Save from the File menu.

  • Right click on the file tab, and select Save.

  • Press Ctrl-S.

TIP

Forgotten how to create directories in Dreamweaver MX? Here's a reminder: In the Files window select the directory in which the new directory is to be created, right-click in the file pane below, and select New Folder.


Executing Templates

Now, let's test the code. There are several ways to do this. The simplest is to right-click on the file in the Files window and select Preview in Browser (selecting your browser off the list).

You may also execute the page directly yourself. Simply open your Web browser and go to this URL:

http://localhost:8500/ows/8/hello1.cfm

TIP

Not using the integrated Web server? See the note at the start of this chapter.


You should see a page like the one in Figure 8.1. I admit that this is somewhat anticlimactic, but wait; it'll get better soon enough.

Figure 8.1. ColdFusion-generated output usually is viewed in any Web browser.


There's another way to browse the code you write. Assuming it is a page that can be executed directly (meaning it is not one that needs to be processed after another pagefor example, a page that expects to be processed after a form is submitted), you can browse it directly in Dreamweaver MX by switching to Design view and activating Live Data view as seen in Figure 8.2.

Figure 8.2. If configured correctly, you'll be able to browse much of your Cold Fusion code within Dreamweaver itself.


NOTE

For Live Data view to work, your site must be configured so that Dreamweaver knows how to pass the page to ColdFusion for processing. Sites, and how to define them, are explained in Chapter 2, "Introducing Macromedia Dreamweaver MX;" refer to that chapter if necessary.


Templates Explained

I promised to explain why ColdFusion files are often referred to as templates. Chapter 1, "Introducing ColdFusion," explains that ColdFusion pages are processed differently from Web pages. When requested, Web pages are sent to the client (the browser) as is, whereas ColdFusion files are processed and the generated results are returned to the client instead.

In other words, ColdFusion files are never sent to the client, but what they create is. And depending on what a ColdFusion file contains, it likely will generate multiple different outputs all from that same single .cfm filethus the term template.



Macromedia Coldfusion MX 7 Web Application Construction Kit
Macromedia Coldfusion MX 7 Web Application Construction Kit
ISBN: 321223675
EAN: N/A
Year: 2006
Pages: 282

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