如何在变量中包含 ColdFusion 标签并对其进行评估?
我得到了一个可以包含 CF 自定义标签的变量。例如
<cfset a = '<model:sparkline id="1"/>'/>
,我希望将其评估为 HTML 并输出。不知道如何/是否可以做到这一点。
I've get a variable that can contain a CF custom tag. E.g.
<cfset a = '<model:sparkline id="1"/>'/>
And I'd like that to be evaluated into HTML and outputted. Not sure how/if I can do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以修改自定义标签吗?如果是这样,您可以使用
caller
范围在调用页面中设置变量。因此,在自定义标记内,您可以执行
,这将在调用页面的variables
范围中设置值。如果您不想修改自定义标记,则可以使用
将输出保存到变量。例子: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'svariables
scope.If you don't want to modify the custom tag, then you can use
<cfsavecontent>
to save the output to a variable. Example:Sean Coyne 的答案是正确的,前提是导入包含在与 cfsavecontent 标记相同的上下文中:
将导致迷你图自定义标签的动态评估输出。
Sean Coyne's answer is the correct one, provided the import is included within the same context as the cfsavecontent tag:
Will result in the dynamically evaluated output of the sparkline customtag.
输出代码并让它执行是不可能的。 OUTPUT只是输出的意思。它并不意味着“跑”。
让 CF 执行 CF 代码的唯一方法是遵循正常渠道:
* 索取模板;
* 包含一个模板;
* 调用模板作为自定义标签或CFMODULE;
* 调用 CFC 中的方法;
* 还有其他吗?无论如何,你明白了。
因此,如果您有动态创建的代码并想要执行...您需要将其写入文件,然后通过最合适的机制调用它。但请注意:运行这样的动态代码会产生相当大的开销,因为代码需要在运行之前进行编译,而编译并不是该方案中最快的过程。这里要做的“最好”的事情是在需要文件之前尝试编写和编译文件,并且仅重写需要更新的文件。不要每次请求都重新执行。但是,理想情况下,根本不要做这种事情。人们通常可以用不同的方式处理事情。
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.