|
Comments
|
Today's Top SOA Links
BF on CF Hidden Gems
Hidden Gems
By: Ben Forta
Dec. 3, 2001 12:00 AM
With all the talk and buzz surrounding Neo, it's important to keep in mind that ColdFusion 5 is still a relatively new product, one that many users have yet to take full advantage of. We're proud of ColdFusion 5, and rightfully so. It's the most reliable, most scalable, and most powerful ColdFusion yet, as well as the fastest server we've ever created. It's simply the best ColdFusion to date. Although I've dedicated numerous columns this year to ColdFusion 5, I thought it worthwhile to do it once more, this time concentrating on some of the little features and enhancements that many have overlooked - features and enhancements that truly are hidden gems.
Inspecting Variables and Expressions
<CFOUTPUT>#var#</CFOUTPUT> But more complex variables can't be displayed that way. Arrays must be looped through, structures must be expanded, queries must be processed one row at a time, and so on. And it gets worse. Really complex variables (perhaps a structure containing an array of structures containing arrays or queries, or something like that) present developers with complicated coding challenges - not the kind of thing you want to tackle while trying to debug code. And then you need to worry about UI and presentation of this data. Well, not anymore - I'd like to introduce you to your new debugging partner, the <CFDUMP> tag. Originally created for Spectra, this tag became so popular and useful that it made its way back into ColdFusion in CF5. So what does <CFDUMP> do? Simply put, <CFDUMP> dumps expressions, any expressions, to screen in a clean DHTML-based table. Consider this example: <CFDUMP VAR="#fname#"> ColdFusion will display an HTML table with the variable name on the left and its value on the right. The next example is just as simple to invoke, but SESSION.cart is actually a shopping cart structure containing an array of selections. <CFDUMP> displays all the contents in a table that can be collapsed and expanded as needed: <CFDUMP VAR="#SESSION.cart#"> Any data can be dumped with <CFDUMP>. The next example displays all SESSION variables for the active SESSION (SESSION is a structure internally). And if one of those SESSION variables were SESSION.cart, then all of the nested data within that variable would be displayed as well: "CFDUMP VAR="#SESSION#"> <CFDUMP> is a powerful tool, and easy to use, one that you'll wonder how you lived without.
Client-Side Regular Expression Validation
You're probably familiar with <CFINPUT>, the form tag that can be used to perform client-side form validation using automatically generated JavaScript code. <CFINPUT> is very useful, but it has one serious limitation - it can't be extended easily. Let me explain: if you need to flag a field as required, if you need to validate a U.S. phone number, if you need to validate a date, then <CFINPUT> works well. But if you need to validate anything that isn't one of the supported validation types - e-mail addresses, non-U.S. phone numbers, required minimum value lengths, for example - then <CFINPUT> falls flat on its face. Or rather, it used to. In ColdFusion 5 <CFINPUT> has been extended to let you validate just about anything you can imagine. How? By adding one new validation type, Regular Ex-pressions. In CF5 you may use a Regular Expression pattern as the validation rule for your field. ColdFusion will generate the needed JavaScript so the browser won't validate the form field unless the value matches the specified Regular Expression. Let's look at an example. I recently created a form that had to prompt for RGB values (colors specified in amounts of red, green, and blue). RGB values are six characters long - three sets of two hexadecimal values (00 to FF). My form had three text fields, one for each of the three values. Here's the <CFINPUT> used for the first one: <CFINPUT TYPE="text"As you can see, I used VALIDATE="regular_expression" and then provided the Regular Expression to use in the new PATTERN attribute. [A-Fa-f0-9] matches a single character of A through F (upper or lower case) or 0 through 9. The {2,} instructs the browser to accept a minimum of two instances of the previous expression. That, coupled with MAXLENGTH="2", and I have a perfect validation rule. Here's another one - one we probably all wish were a built-in rule. It validates that an e-mail address is formed correctly: <CFINPUT TYPE="text"Here the Regular Expression matches one of more alphanumeric characters (or an underscore) followed by an @ sign, followed by one or more alphanumeric characters (again allowing underscores), followed by a period, then followed by one or more alphanumeric characters. This won't actually check that a specified e-mail address is a valid working address, but it will at least prevent completely invalid addresses from being entered. As you can see, with minimal work you can write Regular Expressions to validate all sorts of things. <CFINPUT> is limited no longer. Note: The Regular Expressions used here aren't the same as those used in the CFML RE functions. The Regular Expressions passed to <CFINPUT> are those supported by JavaScript.
Better Log File Use
That's where the new <CFLOG> tag comes in. <CFLOG> lets you write your own entries to log files (either the standard log files or your own). Look at this example: <CFLOG APPLICATION="yes"This code writes the log text to the Application.log file. If a particular line of code were generating an error, you could insert a <CFLOG> call like this above it so that when browsing the log file you'd have enough information to properly diagnose the problem. When used in conjunction with CF5's new log viewer (in the ColdFusion Administrator), <CFLOG> makes those large log files far more manageable and accessible.
Accessing HTTP Data
Until now. New to ColdFusion 5 is the GetHTTPRequestData() function, which exposes any and all HTTP data. Here's the function call: <CFSET data=GetHTTPRequestData()>GetHTTPRequestData() returns a structure containing headers, raw content, server information, and more. To display the raw content you could simply do this: <CFOUTPUT>The structure also contains a second structure, names headers. You can loop through or search this structure to access every header and value. In other words, using GetHTTPRequestData(), you now have access to any and all relevant data.
Summary
As I stated at the start of this column, ColdFusion 5 is simply the best ColdFusion to date. If you haven't upgraded yet, do so quickly. And if you're already running CF5, look for the little features and enhancements that can make your development easier and more productive. I listed just four here. If you find others you think are significant, let me know. Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||