FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

ColdFusion Worst Practices - Part 1: Misusing CFOUTPUT

By: Ray West

Page 1 of 2

Set for printing

Next

In this series, we will try to highlight some worst practices in the use of ColdFusion syntax, features and options.  These are not necessarily the kinds of things that will stop your site from working, but they are things that create bottlenecks, expose security weaknesses, or make your code difficult to read and maintain. In today's installment, we will look at the misuse of the CFOUTPUT tag.

The CFOUTPUT tag is a container that tells the ColdFusion parser to evaluate expressions that are surrounded by pound signs (##). The expression could be a variable (#myvar#), fields from a database query (#myquery.myfield#), the date (#Now()#), or even simple math (#7+2#). Using CFOUTPUT tells the ColdFusion engine to output to the browser the actual value of that variable, or field, or equation rather than the text contained within the pound signs.

The great thing about CFOUTPUT is that it only evaluates things inside those pound signs. It passes everything else through untouched. That means that:

<p>The time is <cfoutput>#Now()#</cfoutput>.</p>

looks exactly the same in the browser as

<cfoutput><p>The time is #Now()#.</p></cfoutput>

Not a big deal if you are outputting a single piece of information, but very valuable if you have a lot of stuff to present in the browser. For a simple example, suppose you needed to address your user by name when telling them the time. You could do this:

<p>Hello <cfoutput>#namevariable#</cfoutput>. The time is <cfoutput>#Now()#</cfoutput>.</p>

But you would be better off to do this:

<cfoutput><p>Hello #namevariable#.The time is #Now()#.</p></cfoutput>

That is better for a few reasons. First it is much more readable. You can more easily see what is going on there when you need to refer to it later on.

In addition, the ColdFusion parser is only having to switch on and off once. Going back and forth between being inside CFOUTPUT and outside CFOUTPUT can put a strain on the server. Again, not such a problem for a couple of variables, but if you were outputting a whole page of database information you might be switching back and forth twenty or more times. Multiply that by a large number of users and it is a strain on the server that you do not need. Don't wrap the whole page in one big CFOUTPUT tag, but use as few as you reasonably can in the context of your code to get the job done.

Page 1 of 2 1 2 Next


download
Download Support Files


Keywords