How can I have ColdFusion tags in a variable and have them evaluated?
I've get a variable that can contain a CF custom tag. E.g.
<cfset a = '<model:sparkline 开发者_JAVA技巧id="1"/>'/>
And I'd like that to be evaluated into HTML and outputted. Not sure how/if I can do this.
Can you modify the custom tag? If so you can use the caller
scope to set a variable in the calling page. So inside the custom tag you could do <cfset caller.a = "whatever" />
and that will set the value in the calling page's variables
scope.
If you don't want to modify the custom tag, then you can use <cfsavecontent>
to save the output to a variable. Example:
<cfsavecontent variable="a">
<model:sparkline id="1" />
</cfsavecontent>
Sean Coyne's answer is the correct one, provided the import is included within the same context as the cfsavecontent tag:
<cfimport taglib="./tags" prefix="model">
<cfsavecontent variable="a">
<model:sparkline id="1" />
</cfsavecontent>
<cfoutput>#a#</cfoutput>
Will result in the dynamically evaluated output of the sparkline customtag.
It's impossible to OUTPUT the code and have it execute. OUTPUT just means output. It doesn't mean "run".
The only way to get CF code to be executed by CF is to follow normal channels: * request a template; * include a template; * call a template as a custom tag or CFMODULE; * call a method in a CFC; * any others? ANyway, you get the point.
So if you have code that you create dynamically and want to execute... you need to write it to a file and then call it via the most appropriate of those mechanisms. Be warned though: running dynamic code like this has a fair overhead, as the code needs to be compiled before it's run, and compilation is not the fastest process in the scheme of things. The "best" thing to do here is to try to write and compile the file before it's needed, and only re-write the file it it needs updatng. Don't re-do it every request. But, ideally, don't do this sort of thing at all. One can usually approach things a different way.
精彩评论