开发者

Coldfusion: splitting a tag across onRequestStart() and onRequestEnd() in Application.cfc

I'm trying to see if there is a way to split a CFSAVECONTENT ta开发者_StackOverflow中文版g across the onRequestStart() and onRequestEnd() functions in Application.cfc to save the generated HTML of any .cfm page in the application to a variable.

Adding <cfsavecontent variable="html"> to onRequestStart() and adding </cfsavecontent> to onRequestEnd() isn't allowed since the tag must be closed in the function.

Is this even possible to do? I'm trying to avoid hard coding the CFSAVECONTENT this into every .cfm page of the site.

Thanks!


Alex,

You could do something like this in OnRequest (untested, but should work).

<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>


I realize this has an accepted answer already, but another way to accomplish this without using cfinclude would be to use the getPageContext() object in onRequestEnd() to nab the generated content:

<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>

The <cfabort /> is important here because if you don't abort the request, the CF engine will output the generated content again and it will end up sending two copies of the output along.

I've used this method to apply site-wide changes to content on sites in a crunch where finding every instance of the original content wasn't practical or timely enough. It can also be used to send the generated content out to a translation service if needed before being returned to the end-user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜