Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

The Secret Powers of Includes

The Secret Powers of Includes

You've probably been told for years that CFINCLUDE is like a compile-time directive that "pulls code" from another file into your template for reuse. That's wrong. I'll prove it to you. Indeed, have you ever tried to include something other than a CF template? You can. Wonder what including a text or XML file might do?

If you're reusing CFML code, have you ever wished you could reuse a template and have its associated Application.cfm be executed? You can. Not with CFINCLUDE, though. In fact, did you know there are at least 13 ways to reuse code? It's not just CFINCLUDE, custom tags, Application.cfm, and CFLOCATION anymore. (Why do I consider CFLOCATION a means of reuse? I'll discuss this later.)

In this article, we'll see these magical secret powers of includes, some of which apply to CFMX only, some that apply to CF4 and 5, and some that are in BlueDragon only. But the first two assertions above, about whether CFINCLUDE is a compile-time directive and includes other than CF templates, are really not new at all. You may have been laboring under false presumptions for years.

CFINCLUDE As Code Include
I'm willing to bet that you've always been told (and simply accepted) that CFINCLUDE's job was to pull code in from another template for reuse. A classic example may be reuse of headers and footers. You define the needed HTML tags for the header in something like header.cfm, and use it in your template with CFINCLUDE TEMPLATE="header.cfm". The result is that the header appears at the top of the page. Simple, right? Of course, the beauty is that if you ever need to change the included file, you change it once and all files that include it will be effected immediately.

You probably also know that if you wanted to use variables in both the including and included templates, you needed to be careful because they shared variable scopes. Let's assume we have main.cfm that uses CFINCLUDE TEMPLATE="included.cfm". If you set a variable in main.cfm, its value is seen in included.cfm and vice versa. It's important to understand that because if you aren't careful, you might change the value of a variable in included.cfm and not realize it will "flow back" into main.cfm, where the output will be "Sally" (see Figure 1).

 

Most programmers know these things and just accept them as gospel. And it gives all the more credence to the notion that CFINCLUDE "pulls the code" of included.cfm into main.cfm, and then interprets/compiles main.cfm. Indeed, the very notion of "include" files is common in other languages (such as C's #include) and they work just that way, so it's natural to assume they would do so in CFML.

But CFML's include is different. Need proof? For one thing, if it was a "compile-time" directive, you could open a tag in the calling template and close it in the included template. But that's not possible.

Still further proof that it's not a compile-time directive is that if you change the included file, the including file (main.cfm, above) would have to be recompiled to see the changes. That's not what happens.

Indeed, now that CFMX creates compiled Java classes, you can see that for yourself. Look at the list of Java classes created by CFMX (in cfusionmx\wwwroot\WEB-INF\cfclasses). Using Windows Explorer or My Computer (on a Windows machine), sort the list by Modified Date so that the most recent dates are at the top of the list. If you just created the two templates above, and ran them, their corresponding class files would be listed first. (The mapping of CF template names to Java class names in CFMX is beyond the scope of this article.)

The important point to note, however, is that if you change the included file (included.cfm in our example) and run the calling program (main.cfm), you'll see that while the underlying class file for included.cfm is updated, that for the calling file is not. This shows that CFINCLUDE is a runtime - not a compile-time - directive. And the same was true in CF 4 and 5 (and BlueDragon).

Still further proof lies in the fact that you can set the included template name dynamically. "What?" you may exclaim. The TEMPLATE attribute can be a variable? Yes. Try it.

CFINCLUDE As Output Grabber
So what's really happening? It may shock some to learn that CFINCLUDE really works much like a custom tag. Not quite, but perhaps very differently than you've been led to believe. What you get when you CFINCLUDE a template is shared access to the processing of the template (creation of variables, etc.) and the output of that template. It may feel like "inclusion of source code" but it's not, really. That's quite a surprise for many, I'm sure.

But here's something that may surprise many. If you carry this observation (that CFINCLUDE grabs output) to its logical conclusion, then it would seem reasonable that you could use it to pull in more than just CFML. Indeed, even that very phrase prolongs the incorrect assertion. You're not "pulling in" CFML. You're executing it and inserting the output of the named file at the given point in the program. But what if it's not CFML that you're including?

Could you CFINCLUDE an XML file? Sure! Again, it's not about inserting the "code" of that included file, just its output. The only question is whether it makes sense to "include" such a file. But clearly, the notion of CFINCLUDE being just a means to "reuse CFML code" is a limited one.

Some files you include won't display properly unless the MIME type of the page has been set correctly for the given file. And you may want to turn off debugging and any other white-space generating aspects of your CFML page. Here's an example:

<cfsetting showdebugoutput="No">
<cfcontent type="text/plain">
<cfinclude template="test.xml">

If you have trouble seeing the output in Internet Explorer, try sending it to Netscape instead. IE tries to render the XML using a built-in stylesheet. If the output isn't pure XML (as if there are CFML errors being rendered as HTML on the page), IE won't show the page at all.

CFINCLUDE As Content Sender
If you could include an XML document this way, then clearly CFINCLUDE is more than just for code. How about a CFINCLUDE of a PDF file? Will that work? What will happen? Sure it will work, if you preface it with <CFCONTENT TYPE="application/pdf">, in order to set the MIME type. And if the browser is set up for PDF, the document will open in the reader.

That may blow away some who have struggled with (or been prevented from using) CFCONTENT as the means to send the file as well as set the MIME type. (Indeed, I've long felt that while the use of CFCONTENT's FILE attribute should be restricted for security reasons, the TYPE attribute when used alone shouldn't at all.) This does beg the question of when you would choose one over the other.

There is at least one difference between the two: CFCONTENT's FILE attribute accepts a full path (drive letter/directory/file) whereas CFINCLUDE's TEMPLATE attribute permits only a relative path (or can leverage an Administrator mapping). Also, CFCONTENT allows specification of the MIME type in the tag itself, and it stops processing of the page. Clearly CFCONTENT has its purpose. But if you don't need either of those benefits and are precluded from using CFCONTENT, CFINCLUDE is an interesting alternative.

Does the ability to use CFINCLUDE in a similar way open the door to a security issue? If you presume that locking down CFCONTENT locked down any way to "send" files to the browser, I suppose so. Just remember that with both, it's only an exploit for someone able to create CFML code on the server.

CFINCLUDE As File Reader
Here's another interesting twist: What if you used it to read in a text file, surrounding it with CFSAVECONTENT? Would that be the same as CFFILE ACTION="read". Yep. Here's an example, assuming you want to read in a file called test.txt:

<CFSAVECONTENT VARIABLE="input">
<CFINCLUDE TEMPLATE="test.txt">
</CFSAVECONTENT>

<CFOUTPUT>#input#</CFOUTPUT>

Again, this may trouble those who've locked down CFFILE as the only means to read in a file. Of course, there are still other ways to read in files, using CFHTTP and Java classes, so this may not be as big a deal from a security standpoint. But it may still surprise many. Now do you see why I titled the article as I did?

Processing Application.cfm on Include
There's one aspect of CFINCLUDE, and indeed custom tags, that has long bothered some. Well, some may like how it works, while others likely haven't even noticed.

When you CFINCLUDE code or call a CF custom tag, have you ever wished that the included/called template would execute just like a full-fledged CF template, at least with respect to executing any associated Application.cfm (and optional OnRequestEnd.cfm)?

Again, to some this would seem lunacy to want it. But just know that there are cases when it's been desirable. But CFINCLUDE doesn't work that way, nor do custom tags. The closest you could come was using CFHTTP (or CFLOCATION, if you didn't mind changing pages) to cause the user to see the full-fledged output of one template from another.

Am I going to tell you that you can make CFINCLUDE work differently? No. (Though I will point out in a moment that BlueDragon does indeed offer that option.)

What I'll point out first is that there's a little-known fact about one of the new features in CFMX, which some may still not yet know exists, called getpagecontext().include().

If you read the CFMX docs (particularly Chapter 32 of the "Developing CFMX Applications with CFML" manual), you'll learn that this "function" is used to include the output of JSP pages within CFML. I say "function" because the format of the syntax is unlike anything we've ever had in CFML. The closing parens at the end of getpagecontext surround nothing, and the ".include()" that follows takes as its argument the JSP page to be included, as in getpagecontext().include("mypage.jsp").

But this isn't just a curious alternative format, nor is it just for calling JSP pages. It can call CFML templates as well. More important, this form has the unique characteristic of causing the included file to really be processed just like a full-fledged page - and yes, with any Application.cfm (and optional OnRequestEnd.cfm) that would be executed if you browsed the template directly.

This is a staggering discovery for those who need that solution. If you ever catch yourself using CFHTTP against a local file just to get the output of a page because you want it executed "fully" but have its output included inside another page, this is the solution for you. And who knows what other problems may exist for which this may be the solution. Please drop me a line to let me know.

BlueDragon, an alternative platform for running CFML applications, does add a couple of things to make your life easier with respect to this discussion. The new include functionality for processing Application.cfm, discussed above, comes as a benefit of the underlying J2EE server on which CFMX runs. And like CFMX, BlueDragon is also built on the J2EE platform. Indeed, while it doesn't yet support most CFMX tags, BlueDragon does offer Java-based features like this as well as J2EE sessions, the ability for JSP pages to exist alongside your CFML templates, and the ability to integrate with Java in many of the same ways that CFMX can.

And with respect to this discussion of includes, it also adds a couple of things to make your life easier (and they're different from CFMX because they were added to BlueDragon before CFMX came out).

Similarly, while there is an available getpagecontext().forward() in CFMX for server-side redirection (as I discussed in the June 2002 CFDJ article, "New Possibility in CFMX: Server-Side Redirects"), BlueDragon offers the simpler CFFORWARD. This is quite different from CFLOCATION and an important new tool for many applications. But it's the same concept as CFMX's approach. Indeed, to use Java/OO terminology, it's the same "implementation," but a different "interface". Again, New Atlanta had theirs first and it seems MM just didn't think of it. There are just a few such differences where New Atlanta has decided to innovate rather than wait.

While the forward approaches and CFLOCATION may seem more about redirecting control in a program, the fact is that they can be key to reuse of code when designing to the Model-View-Controller paradigm.

13 Ways to Reuse
That leads nicely to the last point I had raised at the beginning: that there are now 13 ways to reuse code in CFML. The traditional ones from CF 4 and 5 and on into MX (and of course BlueDragon) are CFINCLUDE, custom tags, CFMODULE (admittedly, just another way to call custom tags versus the CF_approach), Application.cfm, OnRequestEnd.cfm, CFLOCATION, and CFSCRIPT-based UDFs.

CFMX adds getpagecontext.include() and getpagecontext.forward() as well as CFCs and CFFUNCTION, while BlueDragon adds CFINCLUDE PAGE and CFFORWARD. New Atlanta has committed to supporting CFMX tags and functions in its Release 4 version, due later this year. And who knows, perhaps we'll see CFFORWARD and CFINCLUDE PAGE showing up in CFMX some day. That's one of the great things about competition.

You might even add CFX custom tags to the list of code reuse options, but those don't allow reuse of CFML code or other Web content, which is clearly the focus of this article.

Summary
In any case, whether you use CFMX, CF 4 or 5, or have made the move to BlueDragon, I hope you've learned a lot from this article.

We learned that CFINCLUDE is much more than just a way to "pull in code". It opens doors to interesting similarities to CFCONTENT and CFFILE. And we learned that while CFINCLUDE can't call upon an included page to execute its associated Application.cfm, the new J2EE-based variants in both CFMX and BlueDragon can.

There are certainly a lot of surprises in the secret powers of includes. I hope this may open the doors to some very interesting new application development possibilities. Again, please let me know if it does.

More Stories By Charlie Arehart

A veteran ColdFusion developer since 1997, Charlie Arehart is a long-time contributor to the community and a recognized Adobe Community Expert. He's a certified Advanced CF Developer and Instructor for CF 4/5/6/7 and served as tech editor of CFDJ until 2003. Now an independent contractor (carehart.org) living in Alpharetta, GA, Charlie provides high-level troubleshooting/tuning assistance and training/mentoring for CF teams. He helps run the Online ColdFusion Meetup (coldfusionmeetup.com, an online CF user group), is a contributor to the CF8 WACK books by Ben Forta, and is frequently invited to speak at developer conferences and user groups worldwide.

Comments (7) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
charlie arehart 03/09/07 06:30:37 PM EST

Craig, have you tried closing the browser in which you were testing? Often when doing anything other than HTML, the browser may cache a result (such as when you have an error) so that even a refresh still shows the old error. If this doesn't work, let's not have a long back and forth here in the comments. :-) Drop me a note at charlie at carehart.org.

Craig 03/08/07 07:05:30 PM EST

Hi Charlie,

I tried this out and it didn't work. It was displaying the PDF code in the browser and the Word docs were all scrambled.

Thanks for any suggestions,
Craig

charlie arehart 06/15/06 12:04:54 PM EDT

One more comment: I only recently determined that my trick for using CFINCLUDE to read in a text file has one caveat: if the file being read in has CFML tags in it (like a README file for a CF product), the CFINCLUDE will indeed try to execute those tags, possibly getting errors or doing something otherwise unexpected. Forewarned is forearmed. It still has its place, as discussed in the article.

charlie arehart 06/15/06 10:27:27 AM EDT

I see that in the first figure above, the second box has a mistake. It should be <cfset name="jane"&gtl.

Charlie Arehart 12/03/03 04:14:25 PM EST

Oops, I left something out of the article. In the section, "Processing Application.cfm on Include", discussing how to include a file AND cause its application.cfm to run, I mentioned that BlueDragon, like CFMX, supports getpagecontext().include() and that it does cause the application.cfm to run.

While I suggested also that BlueDragon extended CFINCLUDE to act this way, I never explained it.

BlueDragon has added a PAGE attribute to CFINCLUDE. It, in effect, does the same thing as the getpagecontext().include(), just as our CFFORWARD does the same thing as getpagecontext().forward(). I just forgot to explain the new PAGE attribute in the article, though I did mention it in passing in the final section, "13 Ways to Reuse".

Jeff 07/16/03 01:38:00 PM EDT

Excellent article. Hadn't even thought about the other possibilities with CFINCLUDE. Now that you point them out, I'm sure my subconscious will consider them when I am working on a problem that could use them.

Thanks!

mike 08/02/03 01:14:00 PM EDT

Excellent article. I am always impressed by the writeups in the CFDJ. Good job.

@ThingsExpo Stories
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...
In this presentation, Striim CTO and founder Steve Wilkes will discuss practical strategies for counteracting fraud and cyberattacks by leveraging real-time streaming analytics. In his session at @ThingsExpo, Steve Wilkes, Founder and Chief Technology Officer at Striim, will provide a detailed look into leveraging streaming data management to correlate events in real time, and identify potential breaches across IoT and non-IoT systems throughout the enterprise. Strategies for processing massive ...
SYS-CON Events announced today that Cloud Academy named "Bronze Sponsor" of 21st International Cloud Expo which will take place October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara, CA. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud com...
In his session at Cloud Expo, Alan Winters, an entertainment executive/TV producer turned serial entrepreneur, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to ma...
SYS-CON Events announced today that Enzu will exhibit at SYS-CON's 21st Int\ernational Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enzu’s mission is to be the leading provider of enterprise cloud solutions worldwide. Enzu enables online businesses to use its IT infrastructure to their competitive advantage. By offering a suite of proven hosting and management services, Enzu wants companies to focus on the core of their ...
We build IoT infrastructure products - when you have to integrate different devices, different systems and cloud you have to build an application to do that but we eliminate the need to build an application. Our products can integrate any device, any system, any cloud regardless of protocol," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
SYS-CON Events announced today that CA Technologies has been named "Platinum Sponsor" of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business - from apparel to energy - is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assista...
Multiple data types are pouring into IoT deployments. Data is coming in small packages as well as enormous files and data streams of many sizes. Widespread use of mobile devices adds to the total. In this power panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists looked at the tools and environments that are being put to use in IoT deployments, as well as the team skills a modern enterprise IT shop needs to keep things running, get a handle on all this data, and deliver...
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), provided an overview of various initiatives to certify the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldwide re...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
The Internet giants are fully embracing AI. All the services they offer to their customers are aimed at drawing a map of the world with the data they get. The AIs from these companies are used to build disruptive approaches that cannot be used by established enterprises, which are threatened by these disruptions. However, most leaders underestimate the effect this will have on their businesses. In his session at 21st Cloud Expo, Rene Buest, Director Market Research & Technology Evangelism at Ara...
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
New competitors, disruptive technologies, and growing expectations are pushing every business to both adopt and deliver new digital services. This ‘Digital Transformation’ demands rapid delivery and continuous iteration of new competitive services via multiple channels, which in turn demands new service delivery techniques – including DevOps. In this power panel at @DevOpsSummit 20th Cloud Expo, moderated by DevOps Conference Co-Chair Andi Mann, panelists examined how DevOps helps to meet the de...
When growing capacity and power in the data center, the architectural trade-offs between server scale-up vs. scale-out continue to be debated. Both approaches are valid: scale-out adds multiple, smaller servers running in a distributed computing model, while scale-up adds fewer, more powerful servers that are capable of running larger workloads. It’s worth noting that there are additional, unique advantages that scale-up architectures offer. One big advantage is large memory and compute capacity...
Artificial intelligence, machine learning, neural networks. We’re in the midst of a wave of excitement around AI such as hasn’t been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. Will this time be different? Most likely. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Furthermore, the funding and research going into AI now comes from a wide range of com...