Doug's Resume
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...

heaters
hotels boeken in 7 sec
Engagement Rings
Online Dating Australia




SURF'S UP!
You:
Your Web Site:
<< May, 2008 >>
SMTWTFS
123
45678910
11121314151617
18192021222324
25262728293031
Search Blog

ColdFusion Jobs
Recent Comments
Re: The Perfect Alternative to Gas Powered Vehicles (by Thomas Messier at 5/09 12:47 PM)
Re: Promoting Family Unity: Lowering Your Utility Bills! (by Fernando Lopez at 5/07 10:12 PM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 10:56 AM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/06 10:27 AM)
Re: Why I Hate ORMs (a solicited rant) (by Richard at 5/06 6:50 AM)
Re: Why I Hate ORMs (a solicited rant) (by Sean Corfield at 5/06 1:40 AM)
Re: Why I Hate ORMs (a solicited rant) (by Steve Bryant at 5/05 5:07 PM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/05 4:36 PM)
Re: Why I Hate ORMs (a solicited rant) (by Mark Mandel at 5/05 3:52 PM)
Re: Why I Hate ORMs (a solicited rant) (by dougboude at 5/05 3:42 PM)
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS
Reciprocal Links

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!!!

What letter comes three place(s) before the letter J?
Type your answer exactly three time(s) in the designated box.

Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!