Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When to use <CFOUTPUT>? 2

Status
Not open for further replies.

Albion

IS-IT--Management
Aug 8, 2000
517
US
Am I right in assuming that any time I want to output a value from a query I need to use <CFOUTPUT>#value#</CFOUTPUT>. Am I also right in assuming that if I am using that value inside of another CFML tag I do not need the <CFOUTPUT>, or is it only when I am using that value in a CF Function?

For Example

Would it be

<CFLOOP INDEX="X" FROM="1" TO="#query.RecordCount#">

Or

<CFLOOP INDEX="X" FROM="1" TO="<CFOUTPUT>#query.RecordCount#</CFOUTPUT>">

And if the first is true does this rule hold true for all CFML tags, or only certain tags?

Thanks

-Al
 
you need to use the cfoutput tag anytime you want to output any variable
<cfset myvar = "myValue">
<cfoutput>#myVar#</cfoutput>
that would display:

myValue

to the webpage.

You do not need cfoutput inside a tag
<cfset myOtherVar = "<cfoutput>#myVar#</cfoutput>">
That's wrong.
You don't even need to use pound signs
<cfset myOtherVar = "#myVar#">
<cfset myOtherVar = #myVar#>
<cfset myOtherVar = myVar>
will all work however the last is prefered.

in your example
<CFLOOP INDEX="X" FROM="1" TO="<CFOUTPUT>#query.RecordCount#</CFOUTPUT>">

best practice is:
<CFLOOP INDEX="X" FROM="1" TO="#query.RecordCount#">

here i DID use # signs because it's inside a quote. if you said
<CFLOOP INDEX="X" FROM="1" TO="query.RecordCount"> CF would try to convert the text "query.RecordCount" to a number to replace it with the VALUE of query.RecordCount you need the quotes.



the only time you need an output tag is when you'll be displaying coldfusion data structures to the page.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
now between tags...

<cfquery name = "myQuery" datasource = "myDatasource">
SELECT *
FROM Table
WHERE id = #url.ID#
</cfquery>

the output tags are not required between the cfquery tags because nothing is being OUTPUT to the screen.

<cfloop from = "1" to = "10" index = "i">
<cfoutput>#i#</cfoutput><br>
</cfloop>

in this case the output tags are required to OUTPUT the value of i to the page. without them your output would look like this.
#i#
#i#
#i#
#i#
#i#
#i#
#i#
#i#
#i#
#i#

hardely useful.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Acutally, you only need to call cfoutput once in this particular loop. Put cfoutput outside the cfloop and it will open the output, do the loop, and close the output. If they're inside the loop, it will open and close the output for every loop.
Code:
<cfloop from = "1" to = "10" index = "i">
<cfoutput>#i#</cfoutput><br>
</cfloop>

COULD ALSO BE:

<cfoutput>
<cfloop from = "1" to = "10" index = "i">
#i#<br>
</cfloop>
</cfoutput>



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
yeah he's right. call cfoutput once will cut down on the process time a little (not much here but if to = 100000 it might)

I usualy do it the way ECOBB said
I think my IQ drops when I open TT lately... I'm going to take a TT vacation. aloha guys.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
There is no escape, bombboy...you can't stay away from TT and you know it...once the dt's set in, you'll be back! [thumbsup2]



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
**twitch twitch** dang it's only been two hours.. *twitch*

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Had to weigh in here...

Bombboy didn't suggest it but it might lead to the idea that one would be able to say...

Code:
<cfloop from=1 to=query.recordcount index="i">
...stuff...
</cfloop>

But that's not suggested, I'm not sure how that will fare in cfloop, but in a lot of tags, if a value isn't surrounded in #'s, it is taken as literal.

And that's particularly stupid because you can cause an error doing that if the value has invalid characters.

Ecobb also suggested that

Code:
<cfloop...><cfoutput>...</cfoutput></cfloop>

and this

Code:
<cfoutput><cfloop...></cfloop></cfoutput>

And he's right but since you're apparently new to cfloop, its best to get you in the habit now... the second option is better by far.. you'll see better performance.

I just wanted to weigh in since everyone else got to :)

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Bombboy,

That is exactly what I was looking for. What was confusing was when to use CFOUTPUT and when to use the #. When you start to get into a dynamic language such at CF it tends to be confusing. Sort like trying to learn Microsoft Windows with it's 3 billion different ways to do any one task. You start to think, "Well which is the best to use".

thanks for the help!

-Al
 
coldfusion is nice because of how easy it fits into the web page, but at the same time promotes poor practice.

you can do the same thing with say ASP that you can with CF. I'll do all the same things in asp and CF to show you how CF can make it hard to seperate the CF from HTML.

-------ColdFusion-------------
<cfoutput>
<cfset myName = "Travis">
Hello, my name is #myName#
</cfoutput>

------- same thing in ASP -----------------

<%
myName = "Travis"
response.write("Hello, my name is " & myName)
%>

-------the bad way to do this in coldfusion to show example of the "start and stop" ecobb and webmigit talked about -----------

<cfloop from = "1" to = "2" index = "i">
The current number is <cfoutput>#i#</cfoutput>
</cfloop>

---- same thing in ASP. this will show how bad it is to do it this way -------

<%
for i = 1 to 10
%>
The current Number is <%response.write(i)%>
<%
next
%>



notice how asp starts and stopps processing just to output the plain text? it's more obvious with the asp example because there is a clear seperation of ASP and HTML

--- the good method in CF ---
<cfoutput>
<cfloop from = "1" to = "10" index = "i">
The current number is #i#
</cfloop>
</cfoutput>

--- good method in asp ---
<%
for 1 = 1 to 10
response.write("The current number is " & i)
next
%>

see how it only had to "let go" once?

now this isn't to say you need to use CFOUTPUT to do all of your processing. only when going back and forth with plain text and the CF processing.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Good job dude.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
thanks. :)

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top