Using Functions


This is where it starts to get interesting. CFML (the ColdFusion Markup Language) is made up of two primary language elements:

  • Tags. These perform operations, such as accessing a database, evaluating a condition, and flagging text for processing.

  • Functions. These return (and possibly process) data and do things such as getting the current date and time, converting text to uppercase, and rounding a number to its nearest integer.

Writing ColdFusion code requires the use of both tags and functions. The best way to understand this is to see it in action. Here is a revised hello page. Type Listing 8.2 in a new page, and save it as hello2.cfm in the ows/8 directory.

Listing 8.2. hello2.cfm
 <html> <head>  <title>Hello 2</title> </head> <body> Hello, and welcome to ColdFusion! <br> <cfoutput> It is now #Now()# </cfoutput> </body> </html> 

After you have saved the page, try it by browsing it either in a Web browser or right within Dream-weaver MX. (If using a Web browser the URL will be http://localhost:8500/ows/8/hello2.cfm). The output should look similar to Figure 8.3, except that your date and time will probably be different.

Figure 8.3. ColdFusion code can contain functions, including one that returns the current date and time.


Before we go any further, let's take a look at Listing 8.2. You will recall that when ColdFusion processes a .cfm file, it looks for CFML code to be processed and returns any other code to the client as is. So, the first line of code is

 <html> 

That is not CFML codeit's plain HTML. Therefore, ColdFusion ignores it and sends it on its way (to the client browser). The next few lines are also HTML code:

  <title>Hello 2</title> </head> <body> Hello, and welcome to ColdFusion! <br> 

No ColdFusion language elements exist there, so ColdFusion ignores the code and sends it to the client as is.

But the next three lines of code are not HTML:

 <cfoutput> It is now #Now()# </cfoutput> 

<cfoutput> is a ColdFusion tag (all ColdFusion tags begin with CF). <cfoutput> is used to mark a block of code to be processed by ColdFusion. All text between the <cfoutput> and </cfoutput> tags is parsed, character by character, and any special instructions within that block are processed.

In the example, the following line was between the <cfoutput> and </cfoutput> tags:

 It is now #Now()# 

The text It is now is not an instruction, so it is sent to the client as is. But the text #Now()# is a ColdFusion instruction instructions within strings of text are delimited by number signs (the # character). #Now()# is an instruction telling ColdFusion to execute a function named Now()a function that returns the current date and time. Thus the output in Figure 8.3 is generated.

The entire block of text from <cfoutput> until </cfoutput> is referred to as a "<cfoutput> block". Not all the text in a <cfoutput> block need be CFML functions. In the previous example, literal text was used, too, and that text was sent to the client untouched. As such, you also could have entered the code like this:

 It is now <cfoutput>#Now()#</cfoutput> 

Only the #Now()# expression needs ColdFusion processing, so only it really needs to be within the <cfoutput> block. But what if you had not placed the expression within a <cfoutput> block? Try it; remove the <cfoutput> tags, save the page, and execute it. You'll see output similar to that in Figure 8.4obviously not what you want. Because any content not within a <cfoutput> block is sent to the client as is, using Now() outside a <cfoutput> block causes the text Now() to be sent to the client instead of the data returned by Now(). Why? Because if it is outside a <cfoutput> block (and not within any other CFML tag), ColdFusion will never process it.

Figure 8.4. If expressions are sent to the browser, it usually means you have omitted the <cfoutput> tags.


Omitting the number signs has a similar effect. Put the <cfoutput> tags back where they belong, but change #Now()# to Now() (removing the number signs from before and after it). Then save the page, and execute it. The output will look similar to Figure 8.5. Why? Because all <cfoutput> does is flag a block of text as needing processing by ColdFusion. However, ColdFusion does not process all text between the tagsinstead, it looks for expressions delimited by number signs, and any text not within number signs is assumed to be literal text that is to be sent to the client as is.

Figure 8.5. Number signs (#) are needed around all expressions; otherwise, the expression is sent to the client instead of being processed.


<cfoutput> has another important use when working with database-driven content. More information about that can be found in Chapter 10, "Creating Data-Driven Pages."


Now() is a function, one of many functions supported in CFML. Now() is used to retrieve information from the system (the date and time), but the format of that date is not entirely readable. Another function, DateFormat(), can help here. DateFormat() is one of ColdFusion's output formatting functions, and its job is to format dates so they are readable in all types of formats. Here is a revision of the code you just used (see Listing 8.3); save it as hello3.cfm and browse the file to see output similar to what is shown in Figure 8.6.

Listing 8.3. hello3.cfm
 <html> <head>  <title>Hello 3</title> </head> <body> Hello, and welcome to ColdFusion! <br> <cfoutput> It is now #DateFormat(Now())# </cfoutput> </body> </html> 

Figure 8.6. ColdFusion features a selection of output formatting functions that can be used to better control generated output.


DateFormat() is an example of a function that accepts (and requires) that data must be passed to itafter all, it needs to know which date you want to format for display. DateFormat() can accept dates as hard-coded strings (as in #DateFormat("8/17/2004')#), as well as dates returned by other expressions, such as the Now() function. #DateFormat(Now())# tells ColdFusion to format the date returned by the Now() function.

NOTE

Passing a function as a parameter to another function is referred to as "nesting." In this chapter's example, the Now() function is said to be nested in the DateFormat() function.


DateFormat() takes a second optional attribute, too: a format mask used to describe the output format. Try replacing the #DateFormat(Now())# in your code with any of the following, and try each to see what they do:

  • #DateFormat(Now(), "MMMM-DD-YYYY")#

  • #DateFormat(Now(), "MM/DD/YY")#

  • #DateFormat(Now(), "DDD, MMMM DD, YYYY")#

Parameters passed to a function are always separated by commas. Commas are not used if a single parameter is passed, but when two or more parameters exist, every parameter must be separated by a comma.

You've now seen a function that takes no parameters, a function that takes a required parameter, and a function that takes both required and optional parameters. All ColdFusion functions, and you'll be using many of them, work the same waysome take parameters, and some don't. But all functions, regardless of parameters, return a value.

NOTE

It is important to remember that # is not part of the function. The functions you used here were DateFormat() and Now(). The number signs were used to delimit (mark) the expressions, but they are not part of the expression itself.


I know I've already said this, but it's worth repeating: CFML code is processed on the server, not on the client. The CFML code you write is never sent to the Web browser. What is sent to the browser? Most browsers feature a View Source option that displays code as received. If you view the source of for page hello3.cfm you'll see something like this:

 <html> <head>  <title>Hello 3</title> </head> <body> Hello, and welcome to ColdFusion! <br> It is now 01-Feb-05 </body> </html> 

As you can see, there is no CFML code here at all. The <cfoutput> tags, the functions, the number signsall have been stripped out by the ColdFusion Server, and what was sent to the client is the output that they generated.

TIP

Viewing the generated source is an invaluable debugging trick. If you ever find that output is not being generated as expected, viewing the source can help you understand exactly what was generated and why.




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