Link to home
Start Free TrialLog in
Avatar of lorigottschalk
lorigottschalk

asked on

Storing ColdFusion code in a database and then rendering it correctly when you pull it out of the database

I just want to know if this is possible:  I'd like to store ColdFusion code in a database.  I have a page that pulls data from the database, and I would like it to process the code just as it would on a normal .cfm file.

Ex.

TableName.FieldName = "<cfset a = 1><cfoutput>#a#</cfoutput>"

If I pull this from the database and output it on the page, it should display "1".  Instead it displays #a#.  The weird thing is, it doesn't display the "<cfset a = 1>" or the beginning and ending cfoutput tags, so it looks like it is actually processing that part of the ColdFusion code.  But it won't evaluate the #a# as a variable.

Any ideas if this is possible?
Avatar of mkishline
mkishline

It's not actually processing the coldfusion tags. If you "View Source" on the page, the <cfset> and <cfoutput> are there, but since they are formatted like html, the page doesn't render them in the browser window. Since coldfusion operates server-side, I don't believe that it's possible to store code in the database the way that you are attempting and have it process properly on a page where it's being output. You would be better off utilizing components or including templates to achieve the results you're looking for.
Avatar of lorigottschalk

ASKER

How could you use components or includes to pull off something like this?  If you're returning data from a database, is there a way to get it to process the code via a component or include?

Thanks!
You can use flags returned from the database to perform the kind of processing you want, but the processing itself will be hard-coded either into the included file or the component.

<!--- inc_a_variable.cfm --->
<cfset a = 1 /><cfoutput>#a#</cfoutput>

<!--- a_variable.cfc --->
<cffunction name="display" access="public" output="true" returntype="void">
  <cfset a = 1 />
  <cfoutput>#a#</cfoutput>
</cffunction>

<!--- main.cfm --->
<cfquery name="q" datasource="dbname">
  SELECT flag
  FROM tablename
  WHERE criteria = "met"
</cfquery>

<cfif q.flag EQ "template">
  <cfinclude template="inc_a_variable.cfm" />
<cfelseif q.flag EQ "component">
  <cfinvoke component="a_variable" method="display" />
</cfif>
Although I am not sure why you would want to do it, it is possible.
What you would need to do is read from the database, then write to a file, and finally CFINCLUDE that file.
This is part of a content management system.  The entire site resides in the database, so it would be nice to allow some easy processing of dynamic data via ColdFusion.  Otherwise it defeats the purpose of the CMS.

Thanks for your suggestion.  I'll give it a try to see if it works.
ASKER CERTIFIED SOLUTION
Avatar of danrosenthal
danrosenthal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the suggestions, Dan!
no problem, good luck!