CFInclude Templates Are Parsed Regardless Of File Type

Posted September 8, 2006 at 8:46 AM by Ben Nadel

Tags: ColdFusion

This is in response to James who said that ColdFusion CFInclude tags will only parse files if they have appropriate extensions (such as .cfm). This is to demonstrate that that is incorrect. CFInclude will parse any file looking for ColdFusion tags. James argued that a CSS file would throw errors when it encountered HEX color values starting with # signs. What he is confused about is that # are only evaluated inside ColdFusion tags or CFOutput tags.

To demonstrate this (and my point about using the Include() method of the page context), I am setting up a simple page and an included CSS file. The CSS file has ColdFusion code on it that will randomly change the background image of the body tag.

Here is the css file:

  • <!--- Create array of background images. --->
  • <cfset arrImages = ListToArray(
  • "http://static.flickr.com/87/234968608_4ea0e7acd4.jpg?v=0
  • http://static.flickr.com/89/234088556_33aef45615.jpg?v=0
  • http://static.flickr.com/71/204064957_cb9ae3892a.jpg?v=0
  • http://static.flickr.com/66/203414399_365e7f3c0f.jpg?v=0
  • http://static.flickr.com/27/44228284_659a202c75.jpg?v=0
  • http://static.flickr.com/78/222477508_683586fe13.jpg?v=0
  • http://static.flickr.com/61/234025928_14b1e2451e.jpg?v=0
  • http://static.flickr.com/61/225523900_c3de2ad93d.jpg?v=0",
  • " #Chr( 13 )##Chr( 10 )#"
  • ) />
  •  
  • #body {
  • background-image: url(
  • "<cfset WriteOutput( arrImages[ RandRange( 1, 8 ) ] ) />"
  • ) ;
  • font: 12px verdana ;
  • margin: 30px 30px 30px 30px ;
  • }
  •  
  • #text {
  • background-color: #666666 ;
  • border: 2px solid #AAAAAA ;
  • color: #F0F0F0 ;
  • padding: 10px 10px 10px 10px ;
  • }

As you can see, we create an array of images. Then, in the body tag, we execute the CFSet tag and the WriteOutput() method to dynamically set the body background image. And, just to exhaust the error-producing possibilities, I have used ID selectors (#) and colors to demonstrate that # signs will NOT throw errors.

Then, I have two main files. One uses the CFInclude tag to demonstrate the parsing of the CSS file and one that uses the GetPageContext().Include() method to demonstrate that it will not parse the CSS file:

  • <html>
  • <head>
  • <title>CFInclude Parsing Test</title>
  • <style type="text/css">
  • <!--- Include the CSS file via CFInclude. --->
  • <cfinclude template="styles.css" />
  • </style>
  • </head>
  • <body id="body">
  •  
  • <div id="text">
  • <h1>
  • This check to see if a CSS file will be parsed if included
  • via the CFInclude ColdFusion Tag.
  • </h1>
  •  
  • <p>
  • If the CSS file IS parsed, the body should have a
  • randomized background image on page refresh.
  • </p>
  • </div>
  •  
  • </body>
  • </html>

The one that uses the page context differs ONLY in the include statement:

  • <!--- Include the CSS file via page context. --->
  • <cfset GetPageContext().Include( "styles.css" ) />

To see these at work, check out the two demos:

Using CFInclude Tag

http://www.bennadel.com/resources/demo/1/index.cfm

Using GetPageContext().Include()

http://www.bennadel.com/resources/demo/1/index2.cfm

On the second demo, view the page source to see that the Include() method actually includes the ColdFusion tag without executing it.



Reader Comments

Sep 8, 2006 at 12:18 PM // reply »
8 Comments

Good post - simple, clean and to the point! Also, I thought I'd let you know that code samples are coming through as PRE tags, at least in Bloglines.

One compliant -- the choice of images for the sample is a tad bit NSFW.


Sep 8, 2006 at 12:23 PM // reply »
10,640 Comments

Demain,

Thanks for the comment and for the confirmation of PRE tags in Blog lines.

As for the images. I agree. I write a lot of this stuff early in the morning and my judgement is not that great. I will tone it back significantly. My appologies.


Sep 8, 2006 at 12:25 PM // reply »
8 Comments

Oh, don't get me wrong - I personaly don't have a problem seeing beautiful women - it just might be a bit hard to explain if the wrong person walked by my cube while I was, ummm, testing your sample code. ;-)


Sep 8, 2006 at 12:29 PM // reply »
10,640 Comments

I'll be honest... you're not the first person to mention it ;) I want to have fun, but at the same time, I don't want to exclude anyone, cause ColdFusion rocks hardcore and no one should be uncomfortable in any learning environment. As a solution, I will stick to ladies ('cause it's just fun) but no "questionable" photos. Just good, clean fun.


Sep 9, 2006 at 4:27 AM // reply »
4 Comments

Just gotta say Ben that I found your site a few days ago and am addicted to the way you code and the context in which you explain it all. Certainly makes learning much easier ;)


Sep 9, 2006 at 7:20 AM // reply »
10,640 Comments

David,

I really appreciate that :D All I want to do it learn and share the knowledge. I couldn't be happier that you are into the way I code and explain things. Please let me know if you ever want me to whip up an example for something or help out with a problem that needs to be solved.

Thanks again!


su
Jan 16, 2007 at 11:29 AM // reply »
3 Comments

Is there a coldfusion tag to import html content from external site.

similar to jstl tag <c:import url="${param.url}"/>

I am getting an error on <cfset pc = GetPageContext()>. Does this mean I am using an old version of coldfusion.

Thanks


Jan 16, 2007 at 12:18 PM // reply »
10,640 Comments

Su,

I don't know of any way to import from a remote server, but it might be possible somehow.

If GetPageContext() is breaking then you might be using a pre-MX version of ColdFusion. It was introduced in MX 6 I believe.


Mar 27, 2009 at 7:30 AM // reply »
2 Comments

Hi Ben,

I have the same doubt that su,
Is there any way to import external html into our coldfusion application, like a header or footer?

Thanks in advance.

Best Regards.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »