Enhancing Web Site Maintainability using Templates, Part 3By Christiaan Verwijs
In Part 2 we discussed the theory behind the template system. In this part, we will look at some source code!
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):
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
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:
In this case, if you would use
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:
But this template will also work:
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!
Questions, comments, suggestions? E-mail me at: TekTronix@eveolution.com.
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.