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.
About This Tutorial
Author: Drew Tempelmeyer
Skill Level: Beginner 
 
 
 
Platforms Tested: CFMX,BlueDragon
Total Views: 122,320
Submission Date: March 31, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • using Inhouse framework with the support of AJAX we developed a Content Management System based site now while indexing with the spider the AJAX inherited components such as LIST is not trapped in to SEO please give me a solution asap

  • Might there be a neater or cleaner way to do this utilizing some of the new features in Coldfusion 8 so that a script can be made like this, but without having to know or code what format the urls would be in?

  • It's been a while since I've messed with ColdFusion. Since I've had the chance to open up a web hosting business I've allowed ColdFusion so therefore I'm getting back into it. I remember some time ago that someone mentioned they would like to see a static HTML generator from dynamic pages and one was never posted. Since I've already accomplished this in PHP, it was time for me to conquer ColdFusion also. Since I read his post, I knew exactly how to do it but I never had the time to actually write the tutorial. I have to admit, the iPod kind of made me want to do it also. :)

Advertisement

Sponsored By...
Healing Touch Massage - $39.00 - 50 Minute Deep Tissue Massage Dripping Springs, Texas!