Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Florida web site design



Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< September, 2008 >>
SMTWTFS
123456
78910111213
14151617181920
21222324252627
282930
Search Blog

Recent Comments
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

22 July 2006
CF tags within CFSCRIPT Blocks

Some of us are fans of using CFSCRIPT for certain portions of our code. For myself, I’ll typically create my UDFs (User Defined Functions) in this manner…it just looks cleaner to my eye. There can be challenges to doing this, however, due to the fact that certain commonly used CF tags just do not have equivalent CFSCRIPT function calls, like CFQUERY or CFTHROW. It is, however, still possible to incorporate them.

I think a code sample is worth a thousand words, so consider the following sample:

 

<!--- first, we need to turn the CFQUERY tag into a coldfusion function --->
<cffunction name="cfquery" access="public" returntype="query">
 <cfargument name="dsn" type="string" required="true">
 <cfargument name="sqlstring" type="string" required="true">
 <cfargument name="password" type="string" required="false" default="">
 <cfargument name="username" type="string" required="false" default="">
 <cfif arguments.username IS NOT "" and arguments.password IS NOT "">
  <cfquery
   name="thisquery"
   datasource="#arguments.dsn#"
   username="#arguments.username#"
   password="#arguments.password#">
   #arguments.sqlstring#
  </cfquery>
 <cfelse>
  <cfquery name="thisquery" datasource="#arguments.dsn#">
   #preservesinglequotes(arguments.sqlstring)#
  </cfquery> 
 </cfif>
 <cfreturn thisquery>
</cffunction>

<!--- begin the actual UDFs --->
<CFSCRIPT>
 function getProdDescrip(productid){
  var thisSQL = "select shortdescrip from products where baseid ='" & productid & "'";
  descrip = cfquery(dsn="rrtradingpost",sqlstring=thisSQL);
  return descrip.shortdescrip;
 }
</CFSCRIPT>
<!--- end UDFs --->

<cfset thisDescrip = getProdDescrip('BCPLB')>

<cfoutput>#thisDescrip#</cfoutput>

Step 1 is to turn any needed CF tags into functions using the CFFUNCTION tag, defining the tag’s attributes as arguments to the CFFUNCTION.

Then, within our CFSCRIPT section we can refer to the newly created function by name, passing in the pertinent arguments. That’s it!

One thing to consider with this is that you can't build a sql string that includes the cfqueryparam tag and have it work, so you'll have to compromise on this and just pass in your where clause values in native sql format (single quotes, etc.).

Keep it beautiful.  Doug out.




Posted by dougboude at 12:27 PM | PRINT THIS POST! |Link | 1 comment
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

Re: CF tags within CFSCRIPT Blocks
cflib.org has an entire library of UDFs that do just this. http://cflib.org/library.cfm?ID=17
Posted by Al Everett on February 20, 2007 at 2:57 PM

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

13 plus 10 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!