When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Monday, February 14, 2000

Enhancing Web Site Maintainability using Templates, Part 3

By Christiaan Verwijs

In Part 2 we discussed the theory behind the template system. In this part, we will look at some source code!

- continued -

This is the script I use to compile a template, the only things you have to submit to the function are the SQL-query to build a recordset and the template you want to use (correctly formatted, with slots and parameters):

FUNCTION CompileTemplate(strQuery, strTemplate)
   DIM arrFieldDataArray
   DIM arrSlotArray
   DIM arrParamArray
   DIM intMaxFields
   DIM intMaxItems
   DIM intMinFields
   DIM intMinItems
   DIM intMinSlots
   DIM intMaxSlots
   DIM intMinRecords
   DIM intMaxRecords
   DIM intSlotEndIndex
   DIM intTotalIndex
   DIM intSlotID
   DIM intFieldLength
   DIM intMaxParams
   DIM blnParser
   DIM strSlotValue
   DIM strRemaining
   DIM strCurrentString
   DIM strSlot
   DIM strCompiled
   DIM strResult
   DIM rst
   set rst = CreateObject("ADODB.recordset")
   rst.open ObjConn, strQuery, adOpenForwardOnly

      arrFieldDataArray = rst.getrows
      Exit Function

   arrSlotArray = Split(strTemplate, "[")
   intMaxSlots = UBound(arrSlotArray)
   intMaxRecords = UBound(arrFieldDataArray, 2) + 1
   intMaxFields = UBound(arrFieldDataArray)

   FOR intMinRecords = 0 TO intMaxRecords - 1
      strCompiled = arrSlotArray(0)

      FOR intMinSlots = 1 TO intMaxSlots
         strCurrentString = arrSlotArray(intMinSlots)
         intSlotEndIndex = Instr(strCurrentString, "]")
         intTotalIndex = Len(strCurrentString)
         strSlot = Left(strCurrentString, intSlotEndIndex - 1)
         strRemaining = Mid(strCurrentString, intSlotEndIndex + 1,
                            intTotalIndex - intSlotEndIndex + 1)

         arrParamArray = Split(strSlot, "|")
         intMaxParams = UBound(arrParamArray)
         intSlotID = arrParamArray(0)

         IF CInt(intSlotID - 1) <= CInt(intMaxFields) THEN
            strSlotValue = arrFieldDataArray(intSlotID - 1, 
            strSlotValue = ""
         END IF
         IF intMaxParams >= 1 THEN
            intFieldLength = arrParamArray(1)
            IF intFieldLength > 0 THEN
               strSlotValue = Cut(strSlotValue, intFieldLength)
            END IF 
         END IF
         IF intMaxParams >= 2 THEN
            blnParser = arrParamArray(2)
            IF blnParser THEN
               strSlotValue = RemoveHTML(strSlotValue)
            END IF
         END IF

         strCompiled = strCompiled & strSlotValue & strRemaining
      strResult = strResult & strCompiled
   CompileTemplate = strResult

This is it! The new version of the template-compiler! This script allows you to use parameters within slots and returns the complete HTML-code of all the compiled templates. In my example-script, the two parameters are used to either limit the number of characters in a string or to remove HTML from the data-to-be-inputted. To do this, I called two functions called cut and removeHTML. You can replace these functions with your own, or change what the parameter specifies by writing your own parameter-handler.

If, for example, you want to use the second parameter to specify whether or not the current field is a date, you can use this line:

strSlotValue = FormatDateTime(strSlotValue, vbShortDate)

In this case, if you would use FALSE or TRUE as one of the parameter-values for a slot, you’d specify whether or not the field-value is a date. I’d say, go ahead, try some parameters! If you think you’ve got some really nice ones, let me know!

The function shown above is pretty flexible, in most cases it’ll work just fine if you ‘forget’ to specify a parameter, or don’t place any parameter at all, or no slots!. Another great addition to this function is that it allows double slots. If you want to, you can place two of the same slots inside the template. You can even use different parameters for each slot!!

In other words; a template like this’ll work just fine:

this is slot 1: [1|0|FALSE],
and this is slot 2: [2|5|TRUE],
and this is slot 1 again: [1|10|TRUE]

But this template will also work:

this is slot 1: [1],
and this is slot 2: [2],
and this is slot 1 again: [1]

This concludes the second article about my TAD-engine (Template-Aided-Design). In this article we explored ways to be able to manipulate data before inserting it into template-slots without using ASP within that template. This article solves what is perhaps the biggest problem with database-stored templates. There still is one thing to enhance though: speed.

In my next article, I will discuss the use of template-caching to decrease loading-time and releave server-load. I hope you liked this article and can put it to good use on your own site!

Happy programming!!

Questions, comments, suggestions? E-mail me at: TekTronix@eveolution.com.


  • Download the source code in text format

    Christiaan Verwijs is a 19-year old Software-Engineering student from the Netherlands. whenever he has time left, he works on his own gaming-site (http://www.eveolution.com) which will be launched somewhere within the next 4 months. He learned how to use ASP in less than 8 months – all thanks to 4Guys.

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article