【问题标题】:Coldfusion: splitting a tag across onRequestStart() and onRequestEnd() in Application.cfcColdfusion:在 Application.cfc 中的 onRequestStart() 和 onRequestEnd() 之间拆分标签
【发布时间】:2011-04-07 19:40:57
【问题描述】:

我正在尝试查看是否有一种方法可以在 Application.cfc 中的 onRequestStart() 和 onRequestEnd() 函数之间拆分 CFSAVECONTENT 标记,以将应用程序中任何 .cfm 页面的生成 HTML 保存到变量中。

不允许将<cfsavecontent variable="html"> 添加到 onRequestStart() 并将 </cfsavecontent> 添加到 onRequestEnd(),因为标签必须在函数中关闭。

这甚至可能吗?我试图避免将 CFSAVECONTENT 硬编码到网站的每个 .cfm 页面中。

谢谢!

【问题讨论】:

  • 你看过 onRequest() 吗?
  • 谢谢亨利。我在下面使用 Sean 的代码对 onRequestStart 进行了一些调整以使其正常工作。

标签: coldfusion application.cfc


【解决方案1】:

亚历克斯,

您可以在 OnRequest 中执行类似的操作(未经测试,但应该可以)。

<cffunction name="onRequest" returnType="void">
    <cfargument name="thePage" type="string" required="true">
    <cfsavecontent variable="html">
    <cfinclude template="#arguments.thePage#">
    </cfsavecontent>
    <!--- do whatever you want with the html variable here (for example, output it) --->
    <cfoutput>#html#</cfoutput>
</cffunction>

【讨论】:

【解决方案2】:

我意识到这已经有一个公认的答案,但另一种不使用 cfinclude 来完成此任务的方法是使用 onRequestEnd() 中的 getPageContext() 对象来获取生成的内容:

<cffunction name="onRequestEnd" output="yes">
    <cfargument type="string" name="targetPage" required="true" />
    <cfset var html = getPageContext().getOut().getString() />
    <!--- Manipulate the html variable. --->
    <cfoutput>#html#</cfoutput><cfabort />
</cffunction>

&lt;cfabort /&gt; 在这里很重要,因为如果您不中止请求,CF 引擎将再次输出生成的内容,并最终发送输出的两个副本。

我已使用此方法将站点范围内的更改应用于站点上的内容,此时查找原始内容的每个实例既不实用也不及时。如果需要,它还可用于将生成的内容发送给翻译服务,然后再返回给最终用户。

【讨论】:

    猜你喜欢
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多