Don’t rush it…

Just don’t. Ever.

Good grief, that’s been a frightening few days. And all because I rushed something on Tuesday night that wasn’t that urgent after all. Now, who’s going to place bets that this (finally) teaches me a lesson. 200-1, I reckon.

But it does give me a good excuse to post my first proper code example. This one is classic ASP (VBScript), and is very useful when you need to hard-write a lot of files with only minor changes such as the title, keywords, description and a bit of page content between them. You can always see it in action as well, if you want. Oh, and one more thing, this (and any code peovided on this blog) is given without any guarantees or warranty whatsoever.

' all you need to do to that is create your html template and replace some
' of the elements in it with the codified equivalents. here's a list:
' page title = %%pagetitle%%
' meta keywords = %%pagekeywords%%
' meta description = %%pagedescription%%
' page content = %%pagecontent%%
' level (string of ../'s to the root, for image paths etc) = %%lev%%

' grab the template file
templatepath = "template.htm"
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath(templatepath), 1)
bodytext = f.readall
Set f=Nothing
Set fs=Nothing

' this is the function that is called to create the final HTML code
function getBody(bodytext,pagetitle,pagekeywords,pagedescription,lev,pagecontent)
getbody = bodytext
getbody = "" & vbcrlf & getbody
getbody = replace(getbody,"%%pagetitle%%",pagetitle)
getbody = replace(getbody,"%%pagekeywords%%",pagekeywords)
getbody = replace(getbody,"%%pagedescription%%",pagedescription)
getbody = replace(getbody,"%%lev%%",lev)
getbody = replace(getbody,"%%pagecontent%%",pagecontent)
end function

' start your loop (from a database, for example)
' for each item create the HTML and save the file like this:
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(server.mappath("path/to/your/file.html"))
tfile.Write getBody(bodytext,"Page title","keywords","description","../../../","This is the page content")
set tfile=nothing
set fs=nothing
' go to the next item