Enhancing Web Site Maintainability using Templates, Part 2By Christiaan Verwijs
Read my first article? The one about the templates? Well, this article is part 2 in a serie of articles about template-aided design. A method I use on my own site.
In the previous article I explained the basics of my site-building method, and how to write your own scripts to use it for your site. If you havenít read the previous article, make sure to do so before you start reading this one!
Ready? Letís get on with it!
The method discussed in my first article can greatly improve maintainability and flexibility. However, there was one big downfall. Because the templates and data were both stored in a database, it was not possible to use ASP-scripts within the template. You can try it yourself Ė ASP in a database just isnít going to work. In this article I will discuss a method to allow us to use some functions to format the data entered into the template-slots (in case you forgot: a slot is a number enclosed in two block-quotes which is replaced with data during the compilation of the template). Now, we will never be able to use ASP in the database, but what if we can place parameters in the template which tell the ASP template-compiler what to do with the data before placing it in the template?
This is the template we used in the previous article:
Now, wouldnít it be nice to be able to limit the number of characters displayed in a slot, or to tell the server to first remove HTML-code from the data-to-be-inserted? Or perhaps to format the data as currency or as date-time?
Well, itís possible if we alter our template a bit:
In this new template, Iíve defined two special parameters for each slot (excluding the slotís ID-code). The first parameter specifies the maximum number of characters to display in that specific slot (0 = no limit). The second parameter specifies whether or not the server should remove HTML-code from the data (for my site, Iíve made a special formatting-language called EML, and this tag specifies whether or not to use it Ė perhaps Iíll share the scripts behind this language with the world somewhere in the future).
Strictly speaking, you can replace these parameters with whatever you want, itís also possible to add more parameters, but thatíll require some tuning of the script displayed below.
Ok, so the template now supports slot-parameters, but how do we get the ASP-script to actually do what the parameters dictate? Before giving you the script I used, Iíll explain exactly the proces behind it.
The first thing we want, is an array filled with the data we want to put in the template. I recommend using
GetRows (technical docs)
as shown in the previous example. This array should be 2-dimensional; the first dimension holds the number of
the record, where the 2nd dimension holds the individual column-values for that record. In the case of this
|O. Jackson||Wisconsin, USA||29|
|C. Wagenmaker||Amsterdam, NL||16|
|O. Heinrich||Berlin, GER||63|
The Array-record with address (0, 0) would refer to the value
O. Jackson, address (1, 0) would
Wisonsin, USA and (2, 1) to
Now that we have a array filled with data, we can start compiling templates.
The next step in the proces is to isolate the individual slots and to analyze them. Each slot will, again, be split into its individual parameters so that we can use them later one. Now, the only thing left to do, is to cycle through the slots in the template and to replace them with (when specified - formatted) data. This proces is repeated for every record so that you end up with a whole bunch of compiled templates (1 for each record). This is also the resulting value of the function.
In Part 3 we will look at the script to compile a template!