The CFOUTPUT tag what does it allow you to do? One thing it allows you to do on your
web page is output the value of variables. Any kind of variable!
- A regular ol' variable
- A Client variable
- A Cookie variable
Who knows what else I'm missing!
It also allows you to output the contents of a result set from a database query to
your web page. Don't forget the closing tag!
|
|
|
It is also used with <CFSETTING ENABLECFOUTPUTONLY="Yes"> to reduce bandwidth and
improve performance (more on this later)!
Syntax:
<CFOUTPUT [QUERY="query_name"]
[GROUP="query_column"]
[GROUPCASESENSITIVE="Yes" or "No"]
[STARTROW="start_row"]
[MAXROWS="max_rows_output"]>
</CFOUTPUT>
Attributes:
| Attribute |
Description |
Comments |
| query |
Assign the name of the query to use (generated by the cfquery tag). |
Optional. |
| group |
Specifies the query column to use when you group sets of records together. |
Optional. |
| groupCaseSensitive |
A boolean indicating whether to group by case. The default value for this
attribute is YES. |
Optional. |
| startRow |
Specifies the row from which to start output.
Rows being rows in the query resultset. |
Optional. |
| maxRows |
Specifies the maximum number of rows to display in the output section.
Rows being rows in the query resultset. |
Optional. |
Advice on using the QUERY attribute with the CFOUTPUT tag
When I first learned ColdFusion, I used CFOUTPUT with the QUERY attribute to do all my
outputting of recordsets to my pages. My advice now: don't do this! When you want to mix
output (and you will), this makes things really tricky. If you want to loop through a
recordset, use CFLOOP instead. Trust me on this one.
Outputting Values
Take a ColdFusion variable, or a field in a query record set, place it between two pound
signs (#), and Coldfusion will output the value in that spot ... if its between opening
and closing CFOUTPUT tags. To get a better understanding on how the CFOUTPUT tag works,
take a look a the following two sample pieces of code:
| Listing (A) |
|
Listing (B) |
<cfset sDomainName = "chomer.com">
<html>
<head>
<title>List A (no cfoutput tags)</title>
</head>
<body>
The domain name is: #sDomainName#
</body>
</html>
|
|
<cfset sDomainName = "chomer.com">
<cfoutput>
<html>
<head>
<title>List B (cfoutput tags)</title>
</head>
<body>
The domain name is: #sDomainName#
</body>
</html>
</cfoutput>
|
The Results:
The domain name is: #sDomainName#
|
|
The Results:
The domain name is: chomer.com
|
Explanation:
The example above does Not use the cfoutput tag. When Coldfusion sees something in
between pound signs that is Not inside of cfoutput tags, to it is just some text
in between pound signs! So, it will output that text and the pound signs!
|
|
Explanation:
The example above uses the cfoutput tag. Coldfusion knows if something is
between opening and closing cfoutput tags that has pound signs wrapped around it,
that is some sort of variable, regular, query, or other and to output the value
of the variable, not the variable name itself or the pound signs for that matter!
So, in our little example above, the variable: sDomainName has the value of
chomer.com so that is the value it outputs on that spot
on the page!
|
Using the ENABLECFOUTPUTONLY attribute with the CFSETTING tag
I use this option on any serious Coldfusion project that I am working on. I put the
following line as the first line in my Application.cfm file:
<CFSETTING ENABLECFOUTPUTONLY="Yes">
After running the line above, anything you want outputted to the user's web browser
has to be between cfoutput tags! Putting it in the first line of your Application.cfm file
basically means that your whole web site will function this way! The main advantages of setting
up your site this way? Two things: bandwidth and performance.
- Bandwidth. Let's face it. Most sites are limited to a certain amount of bandwidth
every month. If they go over that limit (alot of visits to their site), the site is
either temperarily shut down, or, you the person paying for the hosting have to
pay your web host more money for that extra usage. If your pages do a lot of processing
of stuff: queries, loops, etc. ... you will notice (when not using the CFSETTING tag
as described above) when you View Source, that there will be alot of blank lines
in the output sent to the user's browser. Ive seen cases where pages and pages of
blankness have been sent! Alot of it is carriage returns and line feed characters, but
also thousands and thousands of spaces. What IS all this? Coldfusion has replaced all
your server sided tags with spaces and linefeeds. For small pages, or pages without
alot of processing of queries, etc., this may be okay... But... You're paying in
bandwidth for all them spaces and what-not... so, why send them? The user
is not going to see them anyway!
- Performance. It's been my experience that the largest bottleneck for performance
of a website is network latency.... not processing on the web server. This isn't
always the case, but I find that it is mostly the case. Just think, if you are
not sending all those thousands and thousands of spaces over the network that the
user will never see anyway, the overall size of the page being loaded will be much smaller.
And hence, the time to load the page will be shorter! I've seen dramatic improvements
in page load time by doing this. The rule of thumb for doing this is: "If its not being
outputted to the browser, keep it outside your cfoutput tags!
Taking into account what we've just been talking about, let's revise the code that was in Listing
(B). Check out below:
Listing (C)
<cfset sDomainName = "chomer.com">
<cfoutput><html></cfoutput>
<cfoutput><head></cfoutput>
<cfoutput><title>List C (cfoutput tags - optimized)</title></cfoutput>
<cfoutput></head></cfoutput>
<cfoutput><body></cfoutput>
<cfoutput>The domain name is: #sDomainName#</cfoutput>
<cfoutput></body></cfoutput>
<cfoutput></html></cfoutput>
If you View Source after running this you will notice how much more dense the output
is. It is harder to read (if you are a human) but it is definitely shorter in length.
Going through this extra trouble is more work but it can really pay off. I suggest that you do
this after you are finished getting everything in your project working. Then, go
back and add all these opening and closing cfoutput tags. Try to leave out any new lines
inside these cfoutput tags. The better you are at this, the smaller the output will be to the
browser and the quicker the page will load. Just make sure that you have one closing cfoutput
tag for every opening cfoutput tag!
|