Using ColdFusion Functions ColdFusion Help

Now that you’ve taken a closer look at your first template let’s make some minor modifications to it. Because the output oi the today variable isn’t the easiest to understand let’s change the format so that the date displayed looks something like Tuesday September 15 1998. Of course the date displayed will reflect the current date. Modify your CFSETtag to look like the following:

<CFSET today – DateFormat(Now(), ‘dddd, mmmm d, yyyy’)

Save your file and then reload the page in your browser

Now your date is in a more useful and readable format (see Figure 1.4), but how’ did you do that? As explained before, the NowO function returns the current timestamp of the server but is difficult to read. To change the format so that it is easier to read, you have to modify the date returned by the NowO function ..So, you used another function called theDate+ormat function. The DateFormat function -\.v.ilml odify any date passed to it to almost any format you want. Its syntax is:

DateFormat(date, format_of_date)

FIGURE 1.4

FIGURE 1.4

Adding Comments to Your Templates

Comments may already be familiar to you because of your background in HTML. They can have many purposes, including the following:

• Allowing the developer to explain his/her code
• Tracking changes within a document
• Tracking who has worked on the document
• Placing reminders to make changes or additions to the code

ColdFusJon comment tags are very similar to HTML comment tags. The only differences between how HTML and CFML comments are processed are the following:

• Coldf’usion code within an HTML comment is processed, whereas ColdFusion code within a CFML comment is treated as plain text.
• CFML comments are not displayed in the source code of the Web page document; they are stripped out before the final HTML file is rendered.

CFML and HTML comments look similar. Here is an HTML comment:

<!– add your HTML comment here -~)

Here is another CFML comment:

<!— add your CFML comment here —)

As you can tell except for the extra dash (-) they are very similar. Also, CFML comments can span many lines just like HTML comments can. It is only necessary that a CFML comment begin with <! — and end with —>.

You are now going to add a CFML comment to your home. cfm file for the purpose of explaining your code. Modify the text between the CFOUTPUT tags in your home. cfm file to reflect the changes indicated in Listing 1.2

<CFOUTPUT>
<!—
The following variable, ‘today, displays the current date in the format:
Tuesday, September 15, 1998
Now() returns the current timestamp (date & time)
Dddd – full day of week
Mmmm – full name of month
D-day of month
Yyyy – year in four digits
—>
<CFSET today – DateFormat(Now(), ‘dddd, mmmm d~yyy”»
<I>#today#</I>
</CFOUTPUT>

You will notice that this comment provided quite a bit of information and spanned many lines. Comments are used extensively in ColdFusion to make the code easier to understand. Because every developer has a different style of coding, comments are very helpful in deciphering what others were trying to do with their code.

Good ColdFusion programmers always add comments to their code. Now if you save and reload your page in the browser you won’t see a change. If you look at the source you still won’t see a change. This is because CFML comments do not appear to the end user even in the source. If you were to change the CFML comment to an HTML comment it would of course appear in the browser’s source code. CFML comments appear only to the developers who have access to the original code.

You added the comment tag so that when you refer to this file later, you will understand how you coded the today variable. ColdFusion comments can be extremely useful in documenting your code while also keeping your coding secrets private and safe from end users.

Where Do We Go from Here?

In this chapter you created your first ColdFusion template. Although it wasn’t very fancy it showed you the basic concepts for coding ColdFusion templates. These concepts are the following:

• Understanding variables
• Creating variables
• Displaying variables
• Understanding functions
• Using functions
• Conunenting your code

In the next chapter you will learn how to pass data from one template to another using HTML forms and hyperlinks .

Posted on November 13, 2015 in Creating Your First Cold Fusion Template

Share the Story

Back to Top
Share This