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

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.


IoT & Smart Cities Stories
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...