GrabSection |
Description: | |
GrabSection, and it's small helper function GrabSectionHTML, are handy to create pages out of templates. They take a section out of a string, and return it, removing the section from the input string. For instance, they can be used to grab a row out of a table, and allow you to reproduce the row as many times as necessary and place the multiple rows back in the web page in the proper place. This function, along with Visual Basic's Replace(), or my Replace function for Visual Basic 5, can be used to very easily create template driving web pages.
Templates can be quite useful in situations where you have a set of pages that don't change frequently enough to warrant server-side creation of the pages, but are database driven, and do need to be changed from time to time. For instance, these sample code pages are created by a similar function then published on the web server. The example at the bottom of this page demonstrates how to use Replace and GrabSectionHTML to fill in a template html webpage. Although the page itself is simple for this example, the same code could be used to drive a much more complex webpage. | |
Code: | |
'GrabSection: Grabs a section out of a string, based off a start and ' end tag, and replaces it with another string, returning the ' characters between the start and end tag (via the sSection ' variable). The sInput variable is modified to remove the section. Public Sub GrabSection(ByRef sInput As String, sStart As String, _ sEnd As String, sReplace As String, ByRef sSection As String) 'sInput (input/output) = The string to manipulate 'sStart = The tag to search for at the beginning of the section 'sEnd = The ending tag 'sReplace = The string to replace the entire section with 'sSection (output) = The contents of the section are placed in this Dim nStart As Long Dim nEnd As Long nStart = InStr(1, sInput, sStart) nEnd = InStr(1, sInput, sEnd) If nStart = 0 Or nEnd = 0 Or nEnd < nStart Then Exit Sub End If sSection = Mid(sInput, nStart + Len(sStart), nEnd - _ nStart - Len(sStart)) sInput = Replace(sInput, sStart & sSection & sEnd, sReplace) End Sub 'GrabSectionHTML: Functions like GrabSection, only it names the start ' and end tags based off the Section Name, like ' "<!--SectionNameStart-->" and "<!--SectionNameEnd-->", and ' replaces the entire section with a token like ' "<!--SectionName-->". The contents of the section are returned ' via the sSection variable, and sInput is modified to remove the ' section. Public Sub GrabSectionHTML(ByRef sInput As String, ByRef sSection As _ String, sSectionName As String) 'sInput (input/output) = The string to manipulate 'sSection (output) = The contents of the section are placed in this 'sSectionName = The name of the section GrabSection sInput, _ "<!--" & sSectionName & "Start-->", _ "<!--" & sSectionName & "End-->", _ "<!--" & sSectionName & "-->", _ sSection End Sub | |
Sample Usage: | |
'Example program for GrabSectionHTML Sub Main() Dim sFileData As String 'The contents of the file Dim nFileNum As Long Dim sSectionRow As String 'The contents of each row Dim vArray As Variant 'Array of what we want in the table Dim vItem As Variant 'Used to loop through the array Dim sTemp As String Dim sOutput As String 'Populate the array with some dummy data vArray = Array("One", "Two", "Three", "Four") 'Load the file nFileNum = FreeFile Open "input.html" For Binary As #nFileNum sFileData = Input(LOF(nFileNum), #nFileNum) Close #nFileNum 'This is what input.html looks like for this example: '<html><head><title>Example Webpage</title></head><body> '<table border=1> '<!--RowStart--> '<tr><td><!--CellInfo--></td></tr> '<!--RowEnd--> '</table><hr> 'Page last updated: <!--LastUpdated--> '</body></html> 'Now that the file is loaded, grab the section called "Row" out of ' it. GrabSectionHTML will replace the entire section with a ' comment consisting of just the section name, this can be used ' later to use Replace() to put the section back in place. GrabSectionHTML sFileData, sSectionRow, "Row" 'Loop through each item in the array, and create a copy of the ' row, replacing the token with the current value For Each vItem In vArray 'Copy the section sTemp = sSectionRow 'Replace the token with the current value sTemp = Replace(sTemp, "<!--CellInfo-->", vItem) 'Append the result to our output sOutput = sOutput & sTemp Next 'Now that we've created the replacement for the section, go ahead ' and use Replace() to drop it in sFileData = Replace(sFileData, "<!--Row-->", sOutput) 'And replace the last updated token sFileData = Replace(sFileData, "<!--LastUpdated-->", _ Format(Now, "mm/dd/yyyy hh:mm:ss")) 'And finally, output the result to a file nFileNum = FreeFile Open "output.html" For Output As #nFileNum Print #nFileNum, sFileData; Close #nFileNum End Sub |