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
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
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...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
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...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...