|
|
By now, you know that ColdFusion files have a .cfm extension. These files contain ColdFusion Script, a tag-based markup language, generally called CFML. CFML (ColdFusion Markup Language) is similar to HTML (Hypertext Markup Language), which is also tag-based, but CFML also includes built-in functions similar to those built into ASP, PHP, and others.
As you do with these other languages, you create an application file, and the server interprets that file and delivers dynamically created HTML to the user.
Unlike some other web development languages, ColdFusion doesn't include constructs such as the Request object in ASP. Instead, it simplifies the day-to-day operations of development by consolidating many steps into single tags.
Using ColdFusion Script and CFML, you can create dynamic websites that can be as complex as you want to make them. Let's take a brief look at some of the more common tags and commands to get you started in your ColdFusion development.
The pound (#) symbol has special meaning to ColdFusion. It is used in pairs around variable names when those variables appear by themselves and not necessarily as part of a ColdFusion function or command. For example, the simple code in Listing 11.1 tells ColdFusion to store the string "Darren"
in the variable cMyName and display it. You can see the results in Figure 11.13.
Figure 11.13: The simple output from our script displays the contents of the variable cMyName.
On the CD-Rom We strongly encourage you to key in all the code to create the example files yourself. To check your work, compare your files with the example files on the CD accompanying this book.
Listing 11.1: HELLOWORLD.CFM
<html> <head> <title>HelloWorld</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <cfset cMyName = "Darren"> <cfoutput> Hello World and Hello #cMyName# </cfoutput> </body> </html>
The <cfset> tag defines a variable in ColdFusion. It takes the following form:
<cfset variableName = value>
in which value can be either an expression or a literal value. If the variable you're defining already exists, ColdFusion just sets it to the new value. The <cfset> tag is similar to DIM in ASP, except you can immediately assign a value to your variable as you define it.
This tag is a data output tag that is used to display ColdFusion variables, queries, and other operations. You can use several options with the <cfoutput> tag, such as query, group, and startRow. Each of these options (and others) controls things such as where the data that you're going to display comes from, at what row in the data the output should start, and so forth. You can also just use it as we did in our Listing 11.1 to wrap a section of code. This tells ColdFusion that you're including some ColdFusion-specific tags or variables that it needs to interpret. Listing 11.2 and Figure 11.14, in the next section, show you the code and the results of using <cfoutput> with a query. The <cfoutput>
Figure 11.14: The <cfoutput> and the <cfquery> tags work in conjunction to display a list of records.
tag works like Response.write in ASP, but also has the added functionality of being able to loop through a recordset itself
The <cfquery> tag allows you to create and execute a query on a table or other data source. As you can with other ColdFusion tags, you can apply many attributes to the <cfquery> tag, but in its simplest form, as you can see in Listing 11.2, it simply requires a name and a data source. The SQL code to be used in the query is enclosed within the opening and closing tags. The <cfquery> tag combines into one tag what takes in ASP at least four lines of code, plus the SQL statement.
Note | To run this program, you need to create a DSN (Data Source Name) for the sample database, following the instructions in Chapter 10. Then use that DSN in the datasource= statement. |
Listing 11.2: SHOWAUTHORS.CFM
<html> <head> <title>ShowAuthors</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <cfquery name="MyQuery" datasource="Books"> Select FirstName, LastName, AuthorId from tblAuthors </cfquery> <cfoutput query="MyQuery"> <hr width="25%" align="left"> Author_ID: #AuthorID# <br> First Name: #FirstName# <br> Last Name: #LastName# </cfoutput> </body> </html>
This tag works like Response.end in ASP: it simply halts processing of the page. This tag comes in handy when you're debugging a page and want to stop processing at a specific point. Simply throw in a <cfabort> tag where you want the page to stop, and ColdFusion will not process anything else in the page.
|
|