- Read the entire file into our ASP as a variable.
- Replace all the 'template tags' with real data or a default character.
- Write this back to the browser.
' create a link to the file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
' create a link to the file
ls_file_path = Server.MapPath("./template.html")
' open the file
Set ObjTemplateFile = objFSO.OpenTextFile(ls_file_path)
' read the entire file into a variable
template_html = ObjTemplateFile.ReadAll
Replace(template_html,"{pageTitle}","My Output Page")
Replace(template_html,"{navigationElements}","<a href='index.html'>Home</a><p><a href='http://www.greggriffiths.org'>my site</a>")
Replace(template_html,"{pageHeading}","This is my Test Page")
Replace(template_html,"{pageContent}","This is content of my sample page")
Naming our placeholders tags as we have done should ensure that we don't replace anything we shouldn't as long as we ensure that the placeholders don't appear in our template anywhere where we don't want them replaced. Although, if we want to show a placeholder tag and no have it replaced, we can simply use the HTML attributes for the curly braces, so that they show up in the output, but will not result in a match in our Replace job.
However, if we don't replace a placeholder with a value, then the placeholder tag will appear in output - as shown in this example. Thus, we need to be sure that we replace ALL of the placeholders in the template, even if we just replace them with a space or an empty string.
If we wanted to use this approach with more dynaic data such as the results of a database, we would need to pass in some value to identify which set of values we wanted to display in our output page, then we could get the data from the database. The following example uses a parameter - pageid - passed in the QueryString :
Dim dbConn
Dim SQLStatement
SQLStatement="SELECT * FROM pageinfo WHERE pageid=" & Request.QueryString("pageid")
' create a link to the file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
' create a link to the file
ls_file_path = Server.MapPath("./template.html")
' open the file
Set ObjTemplateFile = objFSO.OpenTextFile(ls_file_path)
' read the entire file into a variable
template_html = ObjTemplateFile.ReadAll
' connect to the DB
db_connect
' run the Query, putting the results into a results set
set results=dbConn.execute(SQLStatement)
' process the results
while not (results.EOF)
Replace(template_html,"{pageTitle}",results("pageTitle"))
Replace(template_html,"{navigationElements}",results("navigation"))
Replace(template_html,"{pageHeading}",results("heading"))
Replace(template_html,"{pageContent}",results("content"))
next
' disconnect from the DB
db_disconnect
' send the output back to the client
Response.Write(template_html)
sub db_connect
Dim DSNName="mysite"
Dim DBUserName="me"
Dim DBPassword="you"
' Create a connection object
set dbConn = server.createobject("ADODB.connection")
' Open a connection using the following criteria - DSN Name, UserName, Password
dbConn.open DSNName, DBUserName, DBPassword
end sub
sub db_disconnect
dbConn.close
end sub