How can I generate static HTML from a dynamic ColdFusion page?
One very powerful caching technique is to run your ColdFusion code to generate a dynamic web page, and then write the contents of the dynamic page to a static HTML file. The static page can then be loaded time and time again without the expense of having to rebuild the page.
To acomplish this task, ypou can use use a combination of the ColdFusion <cfsavecontent> and <cffile> tags, or the ColdFusion scheduling engine to write the HTML generated from running a dynamic page to a static HTML file.
The first method to generate and static documents from dynamic content is to use a combination of the ColdFusion <cfsavecontent> and <cffile> tags. The <cfsavecontent> tag stores the generated output from a block of ColdFusion and HTML code into a varaible. So one you have that varaible, it is a simple matter of using the <cffile> tag to write the data to a static HTML file. The following code shows a sample of this technique:
<!--- Create cached contents (HTML) ---> <cfsavecontent variable="cachedOutput"> <html> <head> <title>Cached File Example</title> </head> <body> <h3>Cached File Example</h3> <cfloop index="loopOn" from="1" to="10"> <cfoutput>The loop is on: #loopOn#<br></cfoutput> </cfloop> </body> </html> </cfsavecontent> <!--- Write cached contents (HTML) to file ---> <cffile action="write" file="C:\temp\cachedFile.html" output="#cachedOutput#">
A second way to generate static documents from dynamic content is to use the ColdFusion scheduling engine. Along with the ability to generate static documents from dynamic content, using the ColdFusion scheduling engine gives you the added ability to automatically update your static documents on whatever schedule you may require.
There are two main options for working with the ColdFusion scheduling engine. The first option is by way of the ColdFusion Administrator, and the second is by using the <cfschedule> tag. For our example, we will use this second method.
The real magic of using the ColdFusion scheduling engine to generate static documents from dynamic content lies in the publish attribute. If this attribute is set to yes, then the results of the page the scheduled task runs will be saved to disk (as per the file and path attributes). Beyond this, we are simply telling the ColdFusion scheduling engine what page to run, and how often the page should be ran. Some sample code using this technique would look as follows:
<cfschedule action = "update" task = "createStaticPage" operation = "httpRequest" file = "cachedFile.html" path = "C:\inetpub\wwwroot\" startDate = "2/13/2006" startTime = "12:00 PM" url = "http://127.0.0.1/createPage.cfm" publish = "yes" interval = "3600" resolveURL = "yes">
This question was written by Jeremy Petersen
It was last updated on February 13, 2006.