0

My question could sound like weird, I want to include a template in a CFML script but not as a different file but a string/text/base64 data in the same file. Is that possible or there is a way in CFML? I can do that in PHP with files as base64 data even images base64 encoded. I thought about something like (not working, of course):

<cfset myinclude = ToString(toBinary('PGNmb3V0cHV0PkNvbGRGdXNpb24gMTA8L2Nmb3V0cHV0Pg=='))>

<cfif listfirst(server.coldfusion.productversion) gte 8>
        <cfinclude template = "#myinclude#">
</cfif>

Why would I do that? To overcome errors between different functions with same name in different CF versions (6, 7, 8, 9, 20) but same names, for portability.

bsteo
  • 1,738
  • 6
  • 34
  • 60
  • possible duplicate of [Execute coldfusion code stored in a string dynamically?](http://stackoverflow.com/questions/8062724/execute-coldfusion-code-stored-in-a-string-dynamically) – Adam Cameron Jan 12 '14 at 15:38
  • This is a variation of http://stackoverflow.com/questions/8062724/execute-coldfusion-code-stored-in-a-string-dynamically. One *cannot* execute a string containing CFML. CFML needs to be compiled, so the code needs to exist in a file before runtime. You can put the file on a ramdisk though, I guess, if you felt you must. The better approach here is simply have actual files with the version-specific code in them, and include the version-specific file. – Adam Cameron Jan 12 '14 at 15:38
  • oh, sorry if duplicate, maybe admin can close. – bsteo Jan 12 '14 at 16:46
  • found my answer, I will keep content as base64, write to file, include then delete – bsteo Jan 12 '14 at 16:49

1 Answers1

0

Found a way to make it work, not really what I wanted but works:

<cfset myinclude = "PGNmb3V0cHV0PkNvbGRGdXNpb24gMTA8L2Nmb3V0cHV0Pg==">

<cfif listfirst(server.coldfusion.productversion) gte 8>
        <cfset myincludesource = "A-#CreateUUID()#">
        <cfset fileWrite(expandPath("#myincludesource#.cfm"), ToString(toBinary(myinclude)))>
        <cfinclude template = "#myincludesource#.cfm">
        <cfset fileDelete(expandPath("#myincludesource#.cfm"))>
</cfif>
bsteo
  • 1,738
  • 6
  • 34
  • 60
  • You probably want to do some sort of locking around that, so you don't have multiple requests trying to add/delete the same file at the same time. It'll also be a bit of a pweformance hit having to recompile those files *every request*. – Adam Cameron Jan 12 '14 at 22:07
  • yes, thought about locking. any idea how to not recompile those file every request? maybe using Ajax requests after dropping/including/deleting? – bsteo Jan 13 '14 at 09:18
  • Check to see if the content has changed before writing them, I guess. Dunno how an AJAX-driven approach would help? This is probably scope for a different question, TBH (but search to see if it's already been asked first, as this is not an uncommon requirement ppl try to implement). – Adam Cameron Jan 13 '14 at 09:44