Browse DevX
Sign up for e-mail newsletters from DevX


Warm Up to ColdFusion : Page 2

Macromedia's ColdFusion technology isn't new, but since the release of the MX version, there's been a new surge of interest. This is a great time to learn the basics of the CF environment. This simple guide will get you started with CFML, form-building, databases, and a sample application you can build in no time flat.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Introduction to CFML
Like HTML, CFML code uses tags. For example:

<HTML> <BODY> <CFSET name="Reginald"> </BODY> </HTML>

All CFML tags start with "CF"; HTML tags do not. The example above contains both normal HTML tags (HTML and BODY), and one ColdFusion tag, CFSET. The CFSET tag sets the value of the variable "name" to "Reginald."

Author Note: The capitalization I use in these examples is optional, though it helps to separate the tags from regular text.

A quick word about data types: When dealing with values such as "Reginald" or "235 Main Street", you must use quotation marks around the text. Text values are called strings. Strings are basically a set of varied characters that may or may not include numbers, letters and symbols. This paragraph is one long string. In contrast, when you're using a number (not a representation of a number in text) such as 34 or 67.29494, you don't use quotes. If you're ever at a programming party, use the term "string" instead of "text" to refer to a series of characters and you will look like you know what you are talking about.

The big advantage of code-driven Web documents is that you can programmatically insert values in the server's response. In other words, CFML makes Web responses "dynamic," they can change depending on the user, the situation, the time of day, the requestor's IP address, or any other criteria you like. In contrast, when a user requests a pure HTML file, the response is "static"—that is, the response doesn't change unless the contents of the requested file changes.

Here's a simple example. Suppose you want to display a variable's value in the body of a document. You will need to wrap the variable with CFOUTPUT tags, and put pound signs immediately around the variable.

<HTML> <BODY> <CFSET name="Reginald"> <CFOUTPUT> #name# </CFOUTPUT> </BODY> </HTML>

When the server processes the page, it replaces the CFOUTPUT tag and the #name# variable with the value of the #name# variable—in other words, the name "Reginald" will show up on the page.

Even though the value of the variable "name" is a string, you don't need to put quotes around the variable #name# because ColdFusion is just printing the value to the page.

The CFOUTPUT tag tells the server to insert a variable's value in place of any variables in the format #variable_name#. If that didn't happen, ColdFusion would ignore the pound signs and the variables values and your browser would see #name# and print it on the page.

So, outputting variable values in ColdFusion is easy enough, but what about something a little more intense? Suppose you want to have different responses for different names. Maybe, if the name variable's value is "Reginald," you want to greet him politely. But if the value is not Reginald, then you want to scold the intruder using discouraging words. To test the variable value, you use the CFIF tag. Take a look at the example below:

<CFSET name="Reginald"> <CFIF name EQ "Reginald"> Hello Reginald, it is very nice to see you. Wont you sit down and have some tea with me? <CFELSE> You are not Reginald. I don't know how you got in here, but youd better leave. </CFIF>

The preceding example compares the value of the variable "name" to Reginald to see if they are equal. If so, the server returns the text immediately following the CFIF tag; otherwise the server returns the text following the CFELSE tag intended for non-Reginald users (anything ELSE). Finally, the closing </CFIF> tag stops the processing. The CFELSE tag is a continuation of the CFIF tag, and does not have to be closed.

Note the EQ in the CFIF opening tag, <CFIF name EQ "Reginald">. That stands for EQUAL, which is an operator. In ColdFusion, you use operators to compare values in a conditional statement. Essentially the code means: "If the name equals 'Reginald', then show the text below; otherwise show the 'You are not Reginald....' text." See a working demo here. The example form lets you use any name.

You've seen a simple, if not very practical example of conditionally displaying variable values. Now that you know how to compare and display values, let's move on to forms, and do something a little more useful.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date