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:
Launch code in new window » Download code as text file »
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:
Launch code in new window » Download code as text file »
The one that uses the page context differs ONLY in the include statement:
Launch code in new window » Download code as text file »
To see these at work, check out the two demos:
http://www.bennadel.com/resources/demo/1/index.cfm
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.
Download Code Snippet ZIP File
Comments (8) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Demonstration Of Date Object Formatting Using Date Prototyping
Large Mistake In My Session Management Logic
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.
Posted by Demian on Sep 8, 2006 at 12:18 PM
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.
Posted by Ben Nadel on Sep 8, 2006 at 12:23 PM
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. ;-)
Posted by Demian on Sep 8, 2006 at 12:25 PM
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.
Posted by Ben Nadel on Sep 8, 2006 at 12:29 PM
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 ;)
Posted by David Cooke on Sep 9, 2006 at 4:27 AM
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!
Posted by Ben Nadel on Sep 9, 2006 at 7:20 AM
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
Posted by su on Jan 16, 2007 at 11:29 AM
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.
Posted by Ben Nadel on Jan 16, 2007 at 12:18 PM