The idea behind templates is well known.
Separate content from design.
There are several examples of templates such as Style Sheets and Mail Merge in Word.
Before Active Server Pages, Microsoft had the Internet/Database Connector (IDC).
It let you add a database connection, SQL statement, and record template to an HTML page.
The template contains placeholders that are the names of fields.
For each record in the result set,
the IDC would copy the template and replace the placeholders with values.
Templates in VBWeb is just as simple yet very flexible.
It's all about replacing placeholders.
Even without templates,
a VBWeb script contains some similarities.
Let's review one as an example.
This example simply outputs the result set to an HTML table.
See if you can follow the logic.
Sub PersonList(Optional ErrMsg As Variant)
Dim Sql As String
Dim rs As Recordset
SendLn CGI_CONTENT_TYPE_TEXT_HTML
SendLn "<html><body><h1>People</h1>"
SendLn "<table BORDER=1 CELLSPACING=0 CELLPADDING=5>"
SendLn "<TR><TD><B>Name</B></TD><TD><B>Address</B></TD>"
SendLn "<TD><B>Phone</B></TD><TD> </TD></TR>"
Sql = "SELECT * FROM Person ORDER BY LastName"
Set rs = db.OpenRecordset(Sql, dbOpenSnapshot, dbForwardOnly)
Do Until rs.EOF
SendLn "<tr><td>" & rs!FullName & "</td>"
SendLn "<td>" & rs!Addr & "</td>"
SendLn "<td>" & rs!Phone & "</td>"
Send "<td><A HREF='personedit.exe?PersonId="
SendLn rs!PersonId & "'>EDIT</a>"
Send "<A HREF='persondel.exe?PersonId="
SendLn rs!PersonId & "'>DELETE</a></td></tr>"
rs.MoveNext
Loop
SendLn "</TABLE></body></html>"
End Sub
|
This code is simple, consice, and gets the job done.
But just try to add some style to it.
You can't edit it in an HTML editor, at least not without considerable trouble.
That's reason enough to pull the HTML out into a separate template file.
Let's see what such a template might look like.
<html>
<body>
<h1>People</h1>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=5>
<TR><TD><B>Name</B></TD><TD><B>Address</B></TD>
<TD><B>Phone</B></TD><TD> </TD></TR>
<RecTemplate><TR>
<TD>[FullName]</TD><TD>[Addr]</TD><TD>[Phone]</TD>
<TD><A HREF="personedit.exe?PersonId=[PersonId]">EDIT</A>
<A HREF="persondel.exe?PersonId=[PersonId]">DELETE</A></TD>
</TR>
</RecTemplate>
<Records>
</TABLE>
</body>
</html>
|
Now that looks more like an HTML page.
Do you see the record template and the placeholders in it?
Look for the tags <RecTemplate> and </RecTemplate>.
They mark the beginning and end of the record template.
The placeholders in it are [FullName], [Addr], [Phone], and [PersonId].
The template page and this next example produce the same results as the first example.
let's review the steps in this example.
- Get the template file using the FileContents instruction.
- Pull the record template out using the Extract instruction.
- Replace placeholders with values from the record set using the Replace instruction.
- Send the results back to the visitor using the old Send instruction.
Sub PersonList(Optional ErrMsg As Variant)
Dim Html As String
Dim Sql As String
Dim rs As Recordset
Dim RecTemplate As String
Dim OneRec As String
Dim Recs As String
Html = FileContents("template\personlist.htm")
Sql = "SELECT * FROM Person ORDER BY LastName"
Set rs = db.OpenRecordset(Sql, dbOpenSnapshot, dbForwardOnly)
RecTemplate = Extract(Html, "<RecTemplate>", "</RecTemplate>")
Do Until rs.EOF
OneRec = RecTemplate
OneRec = Replace(OneRec, "[PersonId]", rs!PersonId)
OneRec = Replace(OneRec, "[FullName]", rs!FullName)
OneRec = Replace(OneRec, "[Addr]", rs!Addr)
OneRec = Replace(OneRec, "[Phone]", rs!Phone)
Recs = Recs & OneRec
rs.MoveNext
Loop
Html = Replace(Html, "<Records>", Recs)
SendLn CGI_CONTENT_TYPE_TEXT_HTML
Send Html
End Sub
|
|