Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active June 15, 2023 19:02
Show Gist options
  • Save JamoCA/dda46141cd11517320bdd380bf38f91c to your computer and use it in GitHub Desktop.
Save JamoCA/dda46141cd11517320bdd380bf38f91c to your computer and use it in GitHub Desktop.
getCFHtmlHead - Retrieves (and optionally clears) the CFHTMLHead buffer
<cfscript>
/* 20221028 getCFHtmlHead - Retrieves (and optionally clears) the CFHTMLHead buffer
Reason: Adobe ColdFusion 2021 now clears the internal head buffer after CFContent. Not sure if a bug,
but definitely different & undocumented.
20230615 Updated to support Lucee 5/6.
Mostly from https://pastebin.com/f2560e44d (CF7 CFML from 10/8/2009)
Gist: https://gist.github.com/JamoCA/dda46141cd11517320bdd380bf38f91c
Blog: https://dev.to/gamesover/change-to-coldfusion-2021-cfhtmlhead-cfcontent-1fj8
Tweet: https://twitter.com/gamesover/status/1586795471019745280
BugTracker: https://tracker.adobe.com/#/view/CF-4215634
*/
string function getCFHtmlHead(boolean clear=true) output=false hint="Retrieves (and optionally clears) the CFHTMLHead buffer" {
local.isLucee = structKeyExists(server, "lucee");
local.out = getPageContext().getOut();
while (getMetaData(local.out).getName() eq "coldfusion.runtime.NeoBodyContent") {
local.out = local.out.getEnclosingWriter();
}
if (local.isLucee){
local.output = local.out.getString();
if (arguments.clear){
local.out.clear();
}
} else {
local.field = local.out.getClass().getDeclaredField("appendHeaderBuffer");
local.field.setAccessible( true );
local.buffer = local.field.get(local.out);
local.output = (structkeyexists(local, "buffer")) ? local.buffer.toString() : "";
if (arguments.clear){
local.method = local.out.getClass().getDeclaredMethod('initHeaderBuffer', []);
local.method.setAccessible( true );
local.method.invoke(local.out, []);
}
}
return local.output;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment