Article

Get Started with ColdFusion

Page: 1 2 3 4 5 Next

Breaking Down the Code Sample

<CFSET MyWorld = ‘Hello World’> creates a variable and value on the page you are viewing. In this specific case we’ve created a variable named “MyWorld” and assigned it the value “Hello World”. In ColdFusion, as in all programming languages, the variable is equal to its value. So MyWorld will process as “Hello World”.

Whenever you need to use this variable on this template and this template only, you may “call” the variable into action with the above CFOUTPUT block. Once you leave this page, the variable dies and must either be reset with CFSET, or passed via the query string.

The name I chose, “MyWorld”, is no magic name… it doesn’t have any mystical programming properties at all. You could name your variable “clownshoes” if you felt the desire to do so.

Using the <CFSET> tag as I have above, there are a few basic guidelines that you may wish to follow.

  1. ColdFusion variable names are NOT case sensitive.

  2. ColdFusion variables are typeless. You do not need to declare whether a variable is a string, number, etc.

  3. Consider typing your variable names in “title case” where every word starts with a capital letter, similar to JackPoe or ColdFusion. This just makes it easier to read them. Feel free to adopt any coding practice you see fit, however, you should pick one naming practice and stick to it from here on out.

  4. Never ever use spaces or punctuation in your variable names… ever.

  5. Your variable name should not start with a number.

  6. Make your variable names easy to remember and short (when possible). This makes them easier to remember and quicker to type. Avoid creating variable names like “MyThirdVariableHere”. If you have to type that a few dozen times it will get tiring quite fast.

So, to quickly recap and diagram a typical <CFSET> tag:

 <CFSET MyScript = ‘Hello Papa Smurf’>  
   The tag being used  
     The variable name  
       The value you are assigning  
to the variable

Now let’s look at the <CFOUTPUT> block:

 <CFOUTPUT>#MyScript#</CFOUTPUT>

You will notice that the variable name is encased in double pound signs or hash marks: ##. If you’ll play along with the analogy, these are like ColdFusion’s “fingerprints”. The ColdFusion Server “dusts” a template, and anywhere it finds these “fingerprints”, it says “Ah ha! I need to do something here.”

In this case, the server receives a request to display the value of the variable named “MyWorld”. So, rather than show you the word “MyWorld”, it will show you the value of the variable, which is “Hello World” in this case.

Let’s do another one -- this time we’ll introduce two simple FUNCTIONS.

  • Now() - This function grabs the current date and time from the server, and formats them in a standard time-stamp manner.
  • DateFormat(date, ‘mask’) - Using this function, we will override the default formatting of the date created with Now(), and tell ColdFusion how we want it formatted.

<CFOUTPUT>#DateFormat(Now(), ‘MM/DD/YYYY’)#</CFOUTPUT>

This line of code will take the current date and reformat it to something like 04/12/2003. The cool thing is that you can treat this whole glob of code as regular text, like so:

Hello, Today Is <CFOUTPUT>#DateFormat(Now(),‘MM/DD/YYYY’)#</CFOUTPUT>.

Using this code in this manner, you’ll see:

Hello, Today is 04/12/2003

Add this code to your INDEX.CFM page and try it out. You can also add some font tags or other formatting HTML around this code to achieve your desired effect.

For more uses and formatting options of the DateFormat() function, consult the functions reference section at the end of this article.

You will notice that we nested functions in this code sample.

<CFOUTPUT>#DateFormat(Now(), ‘MM/DD/YYYY’)#</CFOUTPUT>

ColdFusion allows you to combine or “nest” functions forever. There’s no limit to the number of functions you can combine.

Success with ColdFusion is based on two things:

  1. Know your code. Spend the cash to buy a current CFML Language Reference book and study it. It really is interesting reading.

  2. Practice. As with anything, practice makes perfect.

Note that in some cases you may want to display a pound sign to the user. To do this, you need to “escape the pound sign”. This is done by placing two ## together with no information between them. ColdFusion will interpret this as a single character, not a variable to be processed.

For example, if you use the <CFOUTPUT> tag around a table, and in your HTML tags you try to designate a BGCOLOR=”#FFFFCC”, ColdFusion will read this as the start of a process. To properly escape this you would enter BGCOLOR=”##FFFFCC”

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links