Honestly, I have never needed to do this myself, but apparently many ColdFusion users have, and several have asked me for advice in the past few days. So …
If you were to store CFML source code in a database so that it may be retrieved via a simple database query, what would be the best way to execute that code? You could, of course, write the code to a temporary file and then it, but that’s a hack at best. It also may be possible to parse the text so as to evaluate() any expressions, but that won’t work well with tags.
Whether this is a good practice or not is debatable and a separate discussion. For now, has anyone discovered a creative way to do this? If so, please share.

10 thoughts

  1. The only solution would be saving the content to a *.cfm file.
    Although this is a possible solution, people which are looking for this kind of solutions should better take a look at their application, and the way it is build up.

  2. Actually, Micha, you can save CF code to _any_ file name and cfinclude it. It will be processed. If you were to go this route, I’d specifically use a non-CFM name and use a file outside the web root.

  3. Of course, one of the problems with going from the database to the file system and then executing it is that you get the first time compile hit (even tho’ that’s not such a big deal in 6.1) so it’s not a very performant way to deal with the problem.
    I’m with Micha on this – folks should take a look at their application and rethink their solution.

  4. An alternative to the automatic database processing, what about saving the files before-hand and include them programatically. In your web-based site admin, assuming you have one, on save, update the cfml file as well as the DB record. Then to be doube-sure, schedule a job to query the database every day (or hour) and update your files, as some people may update the cf code without checking in to the site.

  5. I’m with Nathan, what would be the need to store the source code in the database? Without more feedback in that area, I’d suggest cffile writing .dbm (just to be different than normal) files and use cffile read to display for altering.
    Perhaps use another solution for indexing and searching aside from a database?

  6. I have posted many threads on the forum about this exact same functionality. I don’t think it is always a matter of re-thinking the application, I built an app that needed to send emails where the email bodies were stored in a database- but the email bodies needed to be customized with the results of a query. Since once you dynamically put the email body on the page- CF will not process it again to fill in the includes. So sadly, I’m left with useing CFSAVECONTENT and then an include to get the bodies to process. I would gladly "re-think" my app if there were a better way to do this but I don’t think there is.

  7. I have this problem as well. I have the body of an email stored in the db, to make it editable by an admin user. So I have
    Dear #UserFirstName#
    bla,bla,bla
    Users knows not to mess with anything in # signs, but when I put this in the cfmail tag from the the db it does’nt work, just sends out the raw cf code
    Any suggestions ?
    Dharmesh
    dharmesh42@hotmail.com

  8. – Chunks of the code could reside in the database and be written out to file only when they changed. This way, repetitive use wouldn’t include the regeneration and compilation cycle each time the page was browsed. Because the information is in a databas

  9. I was able to get the email example to work using the evaluate and de functions.
    <cfquery name=myQuery … >
    select bodyText from table
    </cfquery>
    <cfmail … >
    #evaluate(de(myQuery.bodyText))#
    </cfmail>

Leave a Reply