We help IT Professionals succeed at work.

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

Last Modified: 2013-12-20
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.


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?
Watch Question

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.


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?

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

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

<cfif q.flag EQ "template">
  <cfinclude template="inc_a_variable.cfm" />
<cfelseif q.flag EQ "component">
  <cfinvoke component="a_variable" method="display" />
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.
This one is on us!
(Get your first solution completely free - no credit card required)


Thanks for the suggestions, Dan!
no problem, good luck!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.