ColdFusion UDF Library - evaluateString
December 16th, 2008
«« Back to ColdFusion Library
Category:
SYSTEM
Description:
Executes inline CFML from a string
Parameters:
Returns:
Requires:
Sample Use:
Copy to Clipboard
Required CFC: execute.cfc
Copy to Clipboard
Category:
SYSTEM
Description:
Executes inline CFML from a string
Parameters:
- string (String, required)
The cfml you wish to have execute. - pass (String, Default: )
- context (String, Default: )
- timeout (Numeric, Default: 5)
Returns:
Requires:
Sample Use:
Copy to Clipboard
function evaluateString(string){
var pass = "";
var context = "";
var timeout = 5;
if (arrayLen(arguments) gte 2)pass = arguments[2];
if (arrayLen(arguments) gte 3)context = arguments[3];
if (arrayLen(arguments) gte 4)timeout = arguments[4];
engine = createObject("component","execute");
return engine.parseString(string,pass,context,timeout);
};
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=99&function=evaluateStringRequired CFC: execute.cfc
Copy to Clipboard
<cfcomponent>
<cffunction name="parseString" returntype="string">
<cfargument type="string" name="text" required="yes">
<cfargument name="caller" default="">
<cfargument name="context" default="">
<cfargument name="timeout" default="5">
<cfif not isStruct(caller)>
<cfset caller = structNew()>
</cfif>
<cfset void = server.reScope("variables",caller)>
<cfset path = expandPath("/")>
<cfif len(context)>
<cfif right(context,1) eq "/">
<cfset context = context & "index.cfm">
</cfif>
<cfif fileExists(expandPath(context))>
<cfset relativePath = getDirectoryFromPath(context)>
<cfset physicalPath = expandPath(getDirectoryFromPath(context))>
<cffile action="read" file="#expandPath(context)#" variable="context">
<cfset uuid = createUUID()>
<cfset filename = "#uuid#.cfm">
<cffile action="write" file="#physicalPath#\#filename#" output="#context#<!--#uuid#-->#text#" addnewline="no">
<cfmodule name="applications.realm.bypass" action="register" realm_id="-1" variable="uniqueID">
<cfschedule action="update" task="#uuid#" operation="httprequest" url="http://#cgi.http_host##relativePath##filename#?__bypass_realm=#uniqueID#" startDate="#now()#" startTime="#now()#" publish="yes" path="#physicalPath#" file="#uuid#.html" interval="once">
<cfschedule action="delete" task="#uuid#">
<cfset stop = false>
<cfset counter = 0>
<cfloop condition="not fileExists(physicalPath & uuid & "".html"") or (fileExists(physicalPath & uuid & "".html"") and getFileInfo(physicalPath & uuid & "".html"").size eq 0) and not stop">
<cfset sleep(250)>
<cfset counter = incrementValue(counter)>
<cfif counter gte timeout*4>
<cfset stop = true>
</cfif>
</cfloop>
<cfif not stop>
<cffile action="delete" file="#physicalPath#\#filename#">
<cffile action="read" file="#physicalPath#\#uuid#.html" variable="content">
<cffile action="delete" file="#physicalPath##uuid#.html">
<cfset pos = findNoCase("<!--#uuid#-->",content)>
<cfif pos>
<cfset data = mid(content,pos+len("<!--#uuid#-->"),len(content)-(pos+len("<!--#uuid#-->")))>
<cfelse>
<cfset data = content>
</cfif>
<cfelse>
<cfset data = "Timed Out">
</cfif>
<cfelse>
<cfset data = "">
</cfif>
<cfelse>
<cfset filename = "#createUUID()#.inc">
<cffile action="write" file="#path##filename#" output="<cfoutput>#text#</cfoutput>" addnewline="no">
<cftry>
<cfsavecontent variable="data">
<cfinclude template="/#filename#">
</cfsavecontent>
<cffile action="delete" file="#expandPath("/")##filename#">
<cfcatch type="any">
<cffile action="delete" file="#expandPath("/")##filename#">
<cfsavecontent variable="data">
<cfrethrow>
</cfsavecontent>
</cfcatch>
</cftry>
</cfif>
<cfreturn data>
</cffunction>
<cffunction name="parseFile" returntype="string">
<cfargument type="string" name="template" default="">
<cfif fileExists("#expandPath("./")##template#")>
<cfsavecontent variable="data">
<cfinclude template="./#template#">
</cfsavecontent>
</cfif>
<cfreturn data>
</cffunction>
</cfcomponent>



