Welcome!

Michael Markowski

Subscribe to Michael Markowski: eMailAlertsEmail Alerts
Get Michael Markowski via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: ColdFusion on Ulitzer

CFDJ: Article

CFDJ Feature — Create a Pseudo-dynamicWeb Site with CF

What to do when the web server doesn't support ColdFusion

You have to love a software product that makes developers, even relatively inexperienced ones, productive quickly and empowers them to do seemingly impossible things. The more I work with and learn about ColdFusion the more it seems as if there are no limits to what can be done with it.

I've experienced the "ColdFusion can do anything" effect on a number of occasions after having used it for some programming task that I previously thought was impossible. Just when it seemed I'd hit a dead end and was about to give up, a quick look through the ColdFusion documentation or some other resource produced a tag or function that was just what I needed. And after a few minutes of learning how to leverage my newly discovered tag (or function), the problem was solved and the supposedly impossible task was thrown onto the pile with all the other "impossible" hurdles that I've conquered with ColdFusion. This article will describe how I used ColdFusion to do one such task that, at the time, seemed impossible to me.

Mission: Possible?
As a webmaster for the Air Protection Division at EPA Region 3 I wear many hats. Besides being a ColdFusion developer responsible for keeping our dynamic data-driven intranet applications up and running, I'm also required to develop/maintain a number of static Web sites on which the content doesn't change all that much. By "static" I mean a Web site that resides on a Web server without any ColdFusion (or database) support. I was recently asked by someone in my organization if there was a way to publish data from a large internal database on a static Web site in the form of HTML Web pages. The data for each record in the database would have to be displayed on a single static Web page, so there would be one page for each database record. The data would need to be updated only occasionally, but the site would have to be created in HTML format to ensure maximum accessibility, usability, and consistency with the rest of the site. Oh, and did I mention that the database contained over 700 records? That's over 700 static Web pages that had to be created and then populated with unique content. My initial reaction was "That's impossible" but, as I've come to expect, ColdFusion would prove me wrong and enable me to do the impossible.

Of course, writing and deploying a small ColdFusion application would be the easiest and fastest way for me to address this problem since I am, after all, a ColdFusion developer and this would be a fairly straightforward data drill-down application to write. However, there was a serious problem with the "let's use ColdFusion" approach: there was no ColdFusion (or database) support of any kind on the destination Web server, nor was there likely to be in the future. Therefore, what I needed was a way to create hundreds (or thousands) of static Web pages from the database information automatically and rapidly so that the resulting pages could be served statically. I could use ColdFusion (or any technology) to create the HTML pages that comprise the static Web site, but I couldn't build an actual ColdFusion application because the server lacked ColdFusion support. Now that I think about it, situations like this probably occur all the time because many organizations that, for whatever the reason, don't have ColdFusion support on their Web site still have data that has to be published on those sites. And they can't just upload a large database or spreadsheet file to the site and be done with it because such a "solution" would be inaccessible and pretty much unusable. In my case the final product had to be user-friendly, accessible, easily navigable, and logically organized. In other words, it had to be a standard HTML Web site consisting of several hundred static Web pages.

If You Build It, They Will Come
After a lot of experimentation and a fair amount of trial and error, I discovered that it's actually possible to automatically create an entire static Web site consisting of several hundred unique Web pages using ColdFusion, a database, and a few key CFML tags. Using the technique I developed, static Web pages are constructed "on the fly" from database records, one page per database record, and then written to the ColdFusion server as HTML files. I also realized that I could mimic the functionality of a dynamic data drill-down application in the static Web site by creating a "master" static Web page containing links to the many "details" pages on the site. I call the resulting Web site a "pseudo-dynamic" site because, although it's comprised entirely of static Web pages, the pages are all created (and maintained) automatically with ColdFusion MX 7.

There are two primary ColdFusion tags that enable the creation of documents that can be served statically via the Web: <CFFILE> and <CFDOCUMENT>. The former has been around forever and is extremely versatile. It can be used to create almost any type of text file, including an HTML file. The <CFDOCUMENT> tag is relatively new and is supported only in ColdFusion MX 7; it's used to create PDF and Flashpaper documents. As this article will demonstrate, <CFFILE> and <CFDOCUMENT> can be used to generate any number of unique HTML, PDF, or Flashpaper documents from database information.

ColdFusion File Writing 101
I could write a book about the many and varied uses of the <CFFILE> tag. The <CFFILE> tag has a multitude of capabilities by virtue of its "action" attribute. The action attribute determines how the tag functions and what it can do. When the action attribute is set to "write," the <CFFILE> tag can create a file and write it to a directory on the ColdFusion server. Other valid values of the action attribute include read, copy, move, upload, and append. However, we're only concerned with the tag's "write" attribute here. When writing files to the ColdFusion server with <CFFILE>, the other required attributes of the tag are "file" and "output." As you might expect, the "file" attribute defines the full path and filename of the file you're writing to the server. The content of a file created with <CFFILE> is defined using the "output" attribute of the tag. The value for this attribute can be as simple as a one-word text string or as long and complex as the HTML code for an entire Web page. In other words, by specifying HTML code in its "output" attribute, <CFFILE> can create a static Web page.

The <CFFILE> tag's "output" attribute defines the actual content of the file you're creating. The example code below creates a simple text file named "myCreatedFile.txt," populates it with the customary "Hello World" message, and then writes the file to the ColdFusion server with the filename "myCreatedFile.txt":

<CFFILE ACTION="WRITE" FILE="C:\Inetpub\wwwroot\mySite\myCreatedFile.txt"
OUTPUT="Hello World!">

As shown here, the value of the <CFFILE> tag's "file" attribute consists of the full path to the file you're creating on the server, plus the filename.

Now, let's get back to the task at hand which is, as you'll recall, creating an entire static Web site automatically with ColdFusion. What if we want to use <CFFILE> to create an HTML file instead of a text file? This is not much harder to do. The main difference is that we first use the <CFSAVECONTENT> tag to assign the necessary HTML code to a variable, and then make that variable the value of the <CFFILE> tag's "output" attribute. This sounds a lot more complicated than it really is, so an example should help. Look at the code Listing 1.

Here the <CFSAVECONTENT> tag "stores" the HTML code needed to create our output file in the "myHTMLCode" variable. Then, the <CFFILE> tag writes a file named "myCreatedFile.html" to the server using the code specified in the "myHtmlCode" variable as the file's content. <CFSAVECONTENT> is invaluable in this situation because it lets us save a long complex text string - HTML code in this case - in a variable that can then be used as the value of the "output" attribute in <CFFILE>. This keeps the code neatly organized and readable. Also, unlike the <CFSET> tag, we don't need to escape quotes or worry about other special characters that may be present in the HTML code because the <CFSAVECONTENT> tag automatically takes care of that for us.


More Stories By Michael Markowski

Michael Markowski works for the Air Protection Division at the Environmental Protection Agency and is a Macromedia/Adobe Certified Professional.

Comments (0)

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.