Including Non-CFML Files As CFML Code In ColdFusion
Earlier today, I was talking to the very brilliant Saritha Bollineni about including non-CFML files into a CFML context. This is something that ColdFusion has supported for as long as I can remember. I've looked at this briefly before; but, I've never experimented with configuring this behavior (which you can do in an Application-level setting). As such, I wanted to put together a quick demo in Adobe ColdFusion 2025.
In Big Sexy Poems, this is how I include the dynamically-generated JavaScript and CSS files output by Parcel.js. If you look at one of my layout templates, you'll see this in the HTML head (truncated):
<cfoutput>
<!doctype html>
<html lang="en">
<head>
<!--- These "HTML" files are dynamically generated by the Parcel build. --->
<cfinclude template="/wwwroot/public/main/vendor/vendor.html">
<cfinclude template="/wwwroot/public/main/member/member.html">
</head>
<!--- ... truncated ... --->
</cfoutput>
When my Parcel.js client-side build process runs, it compiles the JavaScript and Less CSS files down into static files with hashed file names. Parcel also generates an HTML file that uses <script> and <link> tags to include said files (with hashed file names). In the above CFML, these are the generated files that I'm including into my CFML template using the <cfinclude> tag.
By default, any included file will be processed as if it were CFML code. But, you can configure this behavior in the Application.cfc ColdFusion component property, compileExtForInclude. To test this, I've created three different templates, each of which renders the current time using timeFormat():
test.cfmtest.htmltest.txt
For the sake of brevity, I'm showing you all three files below in one snippet:
<!--- TEST.CFM --->
<cfoutput>
<p>
[CFML] Time: #timeFormat( now(), "hh:nn:ss tt" )#
</p>
</cfoutput>
<!--- TEST.HTML --->
<cfoutput>
<p>
[HTML] Time: #timeFormat( now(), "hh:nn:ss tt" )#
</p>
</cfoutput>
<!--- TEST.TXT --->
<cfoutput>
<p>
[TXT] Time: #timeFormat( now(), "hh:nn:ss tt" )#
</p>
</cfoutput>
In my Application.cfc, I then include each of these three files in my onRequest() life-cycle event handler using the default ColdFusion settings:
component {
// Define the ColdFusion application settings.
this.name = "CFIncludeTest";
this.applicationTimeout = createTimeSpan( 0, 1, 0, 0 );
this.sessionManagement = false;
this.setClientCookies = false;
// "CFM" and "CFML" files are always processed as ColdFusion code when invoked via the
// CFInclude tag. But, you can tell ColdFusion that other file-extensions should also
// be compiled and executed as CFML when included.
// --
// Caution: in Adobe ColdFusion 2025, this defaults to "*". Meaning, ANY FILE will be
// compiled and executed as CFML code when invoked via the CFInclude tag.
this.compileExtForInclude = "*";
/**
* I process the incoming script request.
*/
public void function onRequest() {
include "./test.cfm";
include "./test.html";
include "./test.txt";
}
}
If we run this version of the ColdFusion application, we get the following output:
[CFML] Time: 02:31:15 PM
[HTML] Time: 02:31:15 PM
[TXT] Time: 02:31:15 PM
Each of the three include tags processed the given file as CFML code, rendering the timeFormat(), regardless of the file extension. This is the default ColdFusion behavior.
Let's narrow down the list of file extensions. If we set the property to:
this.compileExtForInclude = "html,txt";
... we get the following output:
[CFML] Time: 02:33:57 PM
[HTML] Time: 02:33:57 PM
[TXT] Time: 02:33:57 PM
Even though our list of file extensions excludes cfm, we still get the same output. This is because CFML files are always parsed and executed as CFML code regardless of this application setting.
Let's continue to narrow down the list of file extensions. If we set the property to:
this.compileExtForInclude = "html";
... we get the following output:
[CFML] Time: 02:36:08 PM
[HTML] Time: 02:36:08 PM
[TXT] Time: #timeFormat( now(), "hh:nn:ss tt" )#
This time, with txt excluded from the extensions list, the contents of the test.txt file are included as static text. Meaning, the file is transcluded into the current request but the contents are not executed as CFML code.
As a final test, let's set the property to the empty string:
this.compileExtForInclude = "";
Now when we run this code we get the following output:
[CFML] Time: 02:43:30 PM
[HTML] Time: #timeFormat( now(), "hh:nn:ss tt" )#
[TXT] Time: #timeFormat( now(), "hh:nn:ss tt" )#
When the extensions list is empty, CFML templates are the only includes that are parsed and executed as CFML code. All other file types are transcluded as static text.
Setting this property to html is probably something that I should start doing as my default settings. Special thanks to Saritha for prompting me to look into this in more depth.
Want to use code from this post? Check out the license.
Reader Comments
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →