Hi,
I have developed a solution to generate complete cfscript blocks from
advanced data structures in memory. It is based on the cf_objectdump code.
Here is the code of the calling page. Please use your own query instead.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>var2cfml test</title>
</head>
<body>
<!--- Test Query to use within an array, you have to use your own! --->
<CFQUERY NAME="qBeans" DATASOURCE="FastTrack_Solution">
SELECT Bean_ID, Bean_Name, Bean_Description
FROM Beans
</CFQUERY>
<!--- building an advanced datastructure --->
<cfscript>
aTest=ArrayNew(1);
aTest[1]="Kona";
aTest[2]="Cubita";
aTest[3]=ArrayNew(1);
aTest[3][1]="Kenyan";
aTest[3][2]=Structnew();
aTest[3][2].bohne=ArrayNew(2);
aTest[3][2]["BOHNE"][1][1]="Italian";
aTest[3][2].qBeans=qBeans;
</cfscript>
<!--- dump of first structure --->
<cfdump var="#atest#">
<!--- make the cfml code with cf_var2cfml and use a different start_var_name
for later testing, the default returncode is cfml --->
<cf_var2cfml object="#aTest#" start_var_name="aTester">
<!--- write the generated cfml code to disk and include it --->
<cffile action="WRITE" file="D:\Inetpub\wwwroot\soxml\atester.cfm"
output="#cfml#" addnewline="Yes">
<cfinclude template="atester.cfm">
<!--- dump it to see if everything is fine --->
<cfdump var="#atester#">
</body>
</html>
Here is the code for var2cfml.cfm
<CFSETTING ENABLECFOUTPUTONLY="YES">
<!--- Tag to generate dynamic cfml code from an existing advanced structure,
array, query or whatever it will be. Based upon the great cf_objectdump.
Extended and manipulated by [EMAIL PROTECTED] --->
<!--- Setting the start variable name --->
<CFPARAM NAME="attributes.start_var_name" DEFAULT="var">
<cfparam name="attributes.returncode" default="cfml">
<cfparam name="request.aTags" default="#ArrayNew(1)#" type="array">
<!---
INITIALIZE A DEFAULT OBJECT
--->
<CFPARAM NAME="attributes.Object" DEFAULT="No Object Was Passed!">
<!--- MAKE THE OBJECT LOCAL SO INHERIT BELOW DOES NOT OVERWRITE --->
<CFSET LocalObject = attributes.Object>
<!---
CHECK TO SEE IF THE OBJECT IS A WDDX PACKET
--->
<CFTRY>
<CFSET IsPacket = 1>
<CFWDDX ACTION="WDDX2CFML" INPUT="#LocalObject#" OUTPUT="ThePacket">
<CFCATCH>
<CFSET IsPacket = 0>
</CFCATCH>
</CFTRY>
<!--- IF IT IS A PACKET, SEND TO THE LocalObject or Format for output if not
Deserializing --->
<CFIF IsPacket>
<CFIF attributes.Deserialize>
<CFSET LocalObject = ThePacket>
<CFELSE>
<CFSET LocalObject = htmlcodeformat(LocalObject)>
</CFIF>
</CFIF>
<!---
DETERMINE THE TYPE OF OBJECT
SET NECESSARY OUTPUT VARIABLES
--->
<CFIF IsQuery(LocalObject)>
<CFSET Type = "Query">
<cfset
temp=ArrayAppend(request.aTags,"#attributes.start_var_name#=QueryNew(""#Loca
lObject.columnlist#"");")>
<CFELSEIF IsStruct(LocalObject)>
<CFSET Type = "Structure">
<cfset
temp=ArrayAppend(request.aTags,"#attributes.start_var_name#=StructNew();")>
<CFELSEIF IsArray(LocalObject)>
<CFSET Type = "Array">
<cfset
temp=ArrayAppend(request.aTags,"#attributes.start_var_name#=ArrayNew(1);")>
<CFELSE>
<CFSET Type = "Simple">
</CFIF>
<!--- IF A SIMPLE VALUE, CHECK FORMATTING AND OUTPUT THE EXIT --->
<CFIF IsSimpleValue(LocalObject)>
<!--- OUTPUT THE STRING --->
<cfset LocalObject=Replace(LocalObject,"""","""""","All")>
<cfset
temp=ArrayAppend(request.aTags,"#attributes.start_var_name#=""#LocalObject#"
";")>
<!--- EXIT THE TAG --->
<CFEXIT>
</CFIF>
<!---
SWITCH ON THE DIFFERENT TYPES FOR OUTPUT
--->
<CFSWITCH EXPRESSION="#Type#">
<!--- IF A QUERY, DUMP IT TO THE SCREEN --->
<CFCASE VALUE="Query">
<!--- OUTPUT THE COLUMN NAMES --->
<!--- OUTPUT OVER EACH ROW --->
<CFLOOP FROM="1" TO="#LocalObject.recordcount#" INDEX="RowIndex">
<cfset
temp=ArrayAppend(request.aTags,"QueryAddRow(#attributes.start_var_name#);")>
<!--- LOOP THROUGH EACH COLUMN FOR EACH ROW --->
<CFLOOP LIST="#LocalObject.columnlist#" INDEX="ColumnName">
<CFSETTING ENABLECFOUTPUTONLY="NO">
<cfset
temp=ArrayAppend(request.aTags,"QuerySetCell(#attributes.start_var_name#,""#
ColumnName#"",""#Replace(LocalObject[ColumnName][RowIndex],"""","""""","All"
)#"");")>
<!--- <CF_VAR2CFML Object="#LocalObject[ColumnName][RowIndex]#"> --->
</CFLOOP>
</CFLOOP>
</CFCASE>
<!--- IF THE OBJECT IS A STRUCTURE --->
<CFCASE VALUE="Structure">
<!--- DUMP THE VALUE OF EACH KEY --->
<CFLOOP COLLECTION="#LocalObject#" ITEM="KeyName">
<CFSETTING ENABLECFOUTPUTONLY="NO">
<cfset start_var_name=attributes.start_var_name&"["""&KeyName&"""]">
<CF_VAR2CFML Object="#LocalObject[KeyName]#"
start_var_name="#start_var_name#">
</CFLOOP>
</CFCASE>
<!--- IF THE OBJECT IS AN ARRAY --->
<CFCASE VALUE="Array">
<!--- LOOP THROUGH THE ARRAY AND DUMP EACH ELEMENT --->
<CFLOOP FROM="1" TO="#ArrayLen(LocalObject)#" INDEX="Index">
<CFSETTING ENABLECFOUTPUTONLY="NO">
<cfset start_var_name=attributes.start_var_name&"["&index&"]">
<CF_VAR2CFML Object="#LocalObject[Index]#"
start_var_name="#start_var_name#">
</CFLOOP>
</CFCASE>
</CFSWITCH>
<cfparam name="request.cfml_start_code" default="<cfscript>#chr(13)#">
<cfparam name="request.cfml_end_code" default="</cfscript>#chr(13)#">
<cfset
"caller.#attributes.returncode#"=request.cfml_start_code&"#ArrayToList(reque
st.aTags,"#chr(13)#")#"&request.cfml_end_code>
<CFSETTING ENABLECFOUTPUTONLY="NO">
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists