Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / May 2008



Tip: Looking for answers? Try searching our database.

How to execute arbitrary CFML stored in a string variable?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jgolub - 27 May 2008 15:12 GMT
All-

I have some arbitrary CFML stored in a string. (In practice, it is stored in a
database and retrieved into a string variable.) Is there any way to execute it
[i]other than[/i] writing it to a file and using <cfmodule> to execute that
file?

Note that I found a "render" function in the BlueDragon product, which does
what I am looking for, but I need it in CF itself.

Thanks in advance for any help you can offer.

-Josh
Dan Bracuk - 27 May 2008 15:21 GMT
slightly different, write it to a file and use cfinclude to execute it.
jgolub - 27 May 2008 15:25 GMT
[q][i]Originally posted by: [b][b]Dan Bracuk[/b][/b][/i]
slightly different, write it to a file and use cfinclude to execute it.[/q]

True. Let me rephrase -- is there any way to execute it that doesn't involve
writing it to disk first?

-Josh
Ian Skinner - 27 May 2008 15:41 GMT
One note of caution: 9 out of 10 times I have solved this problem, I
have found it was a problem that did not need to be solved.  There with
a bit of rethinking of my system, I removed the need to execute
arbitrary code.

With that said; as well as writing to a file, you should be able to use
a mix of evaluate() and de() [delay evaluate] to execute code in a string.
jgolub - 27 May 2008 16:45 GMT
[q][i]Originally posted by: [b][b]Newsgroup User[/b][/b][/i]
you should be able to use
a mix of evaluate() and de() [delay evaluate] to execute code in a string.
[/q]

Let's assume that I really do need dynamic evaluation of CFML in this
particular situation.

That said, say that the dynamic CFML has been configured to be this:

  <cfloop index="i" from="1" to="10"><cfoutput>#i# </cfoutput></cfloop>

I put it into a string and evaluate the string like this:

<cfset s = '<cfloop index="i" from="1" to="10"><cfoutput>#de("i")#
</cfoutput></cfloop>'>
<cfoutput>#Evaluate( s )#</cfoutput>

using the de() function to delay evaluation of the variable i. This produces
the following error:

[Q]Invalid CFML construct found on line 1 at column 1.  
ColdFusion was looking at the following text:
<
The CFML compiler was processing:
< marks the beginning of a ColdFusion tag.Did you mean LT or LTE? [/Q]

I am pretty sure that Evaluate() takes a Coldfusion [i]expression[/i] and not
CFML.

Other ideas?

-Josh
Dan Bracuk - 28 May 2008 03:53 GMT
[q][i]Originally posted by: [b][b]jgolub[/b][/b][/i]
[Q][I]Originally posted by: [B][B]Dan Bracuk[/B][/B][/I]
slightly different, write it to a file and use cfinclude to execute it.[/Q]

True. Let me rephrase -- is there any way to execute it that doesn't involve
writing it to disk first?

-Josh[/q]
None that are as simple as writing it to a disk first.  As Adam mentioned, you
also delete the file right after you use it.  

Just out of curiousity, what is your objection to this approach?
jgolub - 28 May 2008 14:40 GMT
[q]
None that are as simple as writing it to a disk first.  As Adam mentioned, you
also delete the file right after you use it.  
Just out of curiousity, what is your objection to this approach?
[/q]

I do not object to this approach, per se, especially as it seems to be my only
option :). I was just surprised that a function like BlueDragon's "Render" had
not made it into CF by now. In an ideal world, the execution of CFML would be
agnostic regarding the persistence format (e.g. disk or database) of the code
in question. Right now, it's biased toward disk-based persistence.

But, we all know about ideal worlds...

Thanks for all of your help.

-Josh
Adam Cameron - 28 May 2008 15:37 GMT
> I was just surprised that a function like BlueDragon's "Render" had
> not made it into CF by now.

Well it only went into BD late last year, which was after the most recent
release of CF.

I don't think it's that much of a boon, functionality-wise.  You can
emulate what it does in about 10min with a bit of a code.

1) create a unique file name;
2) write the string to file;
3) include the file;
4) remove the file if it makes sense not to keep it.

I imagine that's all render() is doing.

I don't see it as value-added functionality, to be honest.

Signature

Adam

Grizzly9279 - 27 May 2008 18:27 GMT
You are correct that Evaluate() will handle ColdFusion expressions, and not
CFML.

However, keep in mind that you can invoke function calls (UDFs, CFC methods,
etc) in a ColdFusion expression.  So in-directly, you can dynamically invoke
any CFML code you want within the context of an Evaluate() call, so long as the
CFML you want to invoke is encapsulated in a UDF or CFC somewhere.

If you really do want to store CFML tags/code in a database and parse it on
the fly, you're not going to have much luck I'm afraid.  Your only option
really will be to write it to a file and <cfinclude> it.  Not pretty.  If this
really is your requirement, consider taking a step back and re-thinking this.  
There's probably a better way to approach this.
Adam Cameron - 27 May 2008 19:22 GMT
I can understand the reluctance to jump through all the hoops of getting a
string, writing it to file, including it, removing the file; but that's
about the only option available as far as doing what you want to do.

Can I recommend you write a custom tag (UDF) which wraps all this up for
you, so you can just write it once and then slap it in your code as needs
must.  This'll save some of the horror of having to be undertaking the task
in the first place ;-)

I'd perhaps write the file with a name that is the hash of the code, that
way I could check whether a file with that hash already exists, and simply
include it if it does.  This saves the file-write, file-read, re-compile,
execute process that creating the files on the fly will have.

I'd like to add a "me too" to the positions put forward that I'd really
consider thinking twice about pulling code out of the DB.  Can you not just
write it to file in the first place, and store a reference to the file in
the DB instead?

Signature

Adam

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.