Creating Static HTML Pages from Dynamic Pages
Creating static HTML will help reduce the resources on your server such as the CPU power, memory usage, etc. The content will also be displayed quicker to the visitor since it won't have to be parsed by the server before it is delivered. This helps sites that receive a large amount of visitors to their site. Too much CPU load and memory usage can result in a longer length of time to parse the page. So let's get started.

1. Lets create our index.cfm page which we will be using as a layout.
<html>
<head>
<title>Test Page</title>
</head>

<body>
<cfparam name="url.id" default="home">
You have tried to load the <cfoutput>#url.id#</cfoutput> page.
</body>
</html>

This will basically tell us the URL variable (or query string) of "id." (i.e. index.cfm?id=something)
Now we will need to write the code so we can generate static HTML pages from dynamic pages.

2. Now lets create save.cfm so we can generate the HTML pages
<html>
<head>
<title>Generating Static HTML with ColdFusion</title>
</head>

<body>
<cfscript>
     //Set the URL of your site. For example, www.domain.com (do NOT include http:// or a trailing slash)
     myurl = "localhost";
     //Let's retrieve the current path so we can store the file without problems. Remember, your directory     //must have write permissions
     path = GetDirectoryFromPath(GetCurrentTemplatePath());
     //Lets set the pages that we want to make static. We will convert the extension to html
     pages = arrayNew(2);
     //We need to define the page to load
     pages[1][1] = "index.cfm";
     //For each file, we will need to tell it what file to create (we'll do this for each file)
     pages[1][2] = "index.html";
     pages[2][1] = "index.cfm?id=resume";
     pages[2][2] = "resume.html";
     pages[3][1] = "index.cfm?id=about";
     pages[3][2] = "about.html";
     pages[4][1] = "index.cfm?id=contact";
     pages[4][2] = "contact.html";
     //Now our pages are stored in an array so we can loop the list now.</cfscript>
<cfloop from="1" to="#ArrayLen(pages)#" index="i">
     <cfhttp method="get" url="http://#myurl#/#pages[i][1]#" throwonerror="yes">
     <cftry>
          <cffile action="write" file="#path#/#pages[i][2]#" output="#cfhttp.FileContent#">
          <cfoutput><b>#pages[i][1]# has been converted to #pages[i][2]#</b><br></cfoutput>
          <cfcatch type="any">
               <cfoutput>#pages[i][1]# was NOT converted to #pages[i][2]#<br></cfoutput>
          </cfcatch>
     </cftry>
</cfloop>
</body>
</html>

Make sure you have writable permissions to the directory you're running the file in or it will not work. Writable permissions are set so a script cannot write to the directory. So if possible, CHMOD the directory to 777. If you're on a Windows server, have your hosting company set the permissions to the directory. Once this is complete you're ready to run the script. It is recommended that you set it as a scheduled task, or cron job, so you can be sure to have updated pages every hour. This can be CPU intesive depending on the amount of pages being created, but it may also help you in the long run. If you have any questions or comments, please contact me.

All ColdFusion Tutorials By Author: Drew Tempelmeyer
  • 9 Day Weather Forecast
    This will retrieve the 9 day weather forecast for the specified ZIP code.
    Author: Drew Tempelmeyer
    Views: 16,649
    Posted Date: Friday, May 16, 2003
  • Creating Static HTML Pages from Dynamic Pages
    Static HTML can help reduce the load on the server. This helps resources be used for other things that you may possibly need. Creating static HTML in ColdFusion is a simple task and can simply be done by following this tutorial.
    Author: Drew Tempelmeyer
    Views: 10,338
    Posted Date: Thursday, March 31, 2005
  • Parsing XML With ColdFusion MX
    Want to know how to parse XML with ColdFusion with ease and very basic concepts? Then this is the tutorial for you.
    Author: Drew Tempelmeyer
    Views: 17,259
    Posted Date: Sunday, November 9, 2003
  • Shout Box [LIVE]!
    Ever want a place where people can chat or post their opinions? Ever heard of a shout box? This tutorial will give you an easy example of having a shout box on your site.
    Author: Drew Tempelmeyer
    Views: 20,272
    Posted Date: Saturday, February 22, 2003
  • WHOIS Search
    This will show you how to make a simple WHOIS search that can be easily added to any site.
    Author: Drew Tempelmeyer
    Views: 9,451
    Posted Date: Sunday, April 27, 2003