[thelist] CFMX Tip

Buffington, Michael michael.buffington at opbu.xerox.com
Thu Oct 31 09:40:00 CST 2002


Here comes a long tip, something obscure I just figured out (well, perhaps obscure to me).

<tip type="ColdFusion" author="Michael Buffington">
Do you have a certain fondness for using CFSCRIPT, only to have that fondness dashed to an bitter, cold-blooded guile when you find out that you can't use things like CFQUERY and CFHTTP within CFSCRIPT?

Well, ColdFusion MX is the land of milk and honey, because you can now "simulate" CF tags within CFSCRIPT.

Here's how.

Create a function using the CFFUNCTION tag like so:

<cffunction name="CFQUERY" access="public" returntype="query">
  <cfargument name="SQL" type="string" required="yes">
  <cfargument name="DSN" type="string" required="yes">
  <cfargument name="dbType" type="string" default="">
  <cfquery name="qry"
	datasource="#arguments.DSN#"
	dbtype="#arguments.dbType#">
	#preserveSingleQuotes(arguments.SQL)#
 </cfquery>
 <cfreturn qry>
</cffunction>


Now, within CFSCRIPT:

<cfscript>
  SQL = "SELECT name_id, name FROM FamousPeople WHERE name_id = 3482";
  DSN = "People";
  famousPerson = CFQUERY(SQL:SQL,DSN:DSN);
</cfscript>

The above will execute the CFQUERY *function* created above with the CFFUNCTION tag. Notice how the arguments and values are passed in the third line of the CFSCRIPT block. Where you see SQL:SQL, these are attribute/value pairs. If the SQL variable were named HereIsMySQLString, it would like like this: SQL:HereIsMySQLString

But creating a function wrapping a CF tag, then calling it within the same template is a bit boring, why go to the effort? You shouldn't, unless of course you're placing the CFFUNCTION within a CFC, which can then be invoked within your CFSCRIPT block like so:

<cfscript>
// lets pretend our CFC is call MyCFTagsYo
MyTags = createObject("component","MyCFTagsYo");

SQL = "SELECT name_id, name FROM FamousPeople WHERE name_id = 3482";
DSN = "People";
famousPerson = MyTags.CFQUERY(SQL:SQL,DSN:DSN);
</cfscript>

Incidentally, because the CFQUERY tag has now been turned into a CFC, it could be used by anything that can consume a webservice. Pretty cool stuff!
</tip>



More information about the thelist mailing list