Using FunctionsThis is where it starts to get interesting. CFML (the ColdFusion Markup Language) is made up of two primary language elements:
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.cfmAfter 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.[View full size image] ![]() That is not CFML codeit's plai269. Therefore, ColdFusion ignores it and sends it on its way (to the client browser). The next few lines are als270 code: 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 no275: <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: 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: 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.[View full size image] ![]() Figure 8.5. Number signs (#) are needed around all expressions; otherwise, the expression is sent to the client instead of being processed.[View full size image] ![]() Listing 8.3. hello3.cfm
Figure 8.6. ColdFusion features a selection of output formatting functions that can be used to better control generated output.[View full size image] ![]() NOTEPassing 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:
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. NOTEIt 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: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. TIPViewing 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. |