E-Commerce Solutions: Template-Driven Pages, Part 2 Page 2

By Martin Brown (Send Email)
Posted Sep 13, 2000


You can create as many templates as you like, and have as many different replacement strings as you like. It'll also replace the same string a number of times, useful if you want the page title, and the title displayed within the page to be the same.

There is of course a little problem with this, in that in order for this to work, you need to have a different set of templates that support the %%text%% construct. So, the final trick is to change the way in which we search for the matching string that we want to replace. Instead of using %%text%%, you use a standard SSI construct, using a comment to encapsulate the text to be replaced. For example, we could have a template with:

<font size=+2><b><!--#include perltext=title --></b></font>

Now if you use the template as an SSI include in another document, the 'replacement' text will be ignored, because the SSI system will treat it as a comment. But when parsed by an updated version of our function, the 'title' gets replaced with the desired text.

All you have to do is modify the function to replace the quoted string. I've included the full version of that function below:

sub parse_template
{
    my (,%subs) = @_;

    open(TEMPLATE,"")
        or print "I tried to load <br>\n";
    {
        local $/;
        sh = <TEMPLATE>;
    }
    close(TEMPLATE);

    foreach  (sort keys %subs)
    {
         = quotemeta '<!--#include perltext=' .  . ' -->';
        sh =~ s//{}/g;
    }
    return sh;
}

I've used quotemeta here to make sure that the whole string is suitable for use as a search string in the regular expression.

Using Python

Although Python lags slightly behind Perl, it's use as a CGI scripting technology is increasing, especially with the popularity of the XML tools (which have been completely rewritten in the new Python 2.0 beta) and complete web serving solutions such as ZOPE (Z-Objects Publishing Environment). The sequences with Python is much the same, we slurp in the text and then use the re (regular expression) module to do the replacement on each line. The actual function looks like this:


import re

def parse_template(file,sub):
    try:
        tempfile = open(file)
    except:
        return ''
    template = tempfile.read()
    for subtext in sub.keys():
        template = re.sub('%%'+subtext+'%%',sub[subtext],template)
    return template

We call it in much the same way as with Perl, supplying the name of the template and a dictionary that contains the replacement strings:

print parse_template('t2.html',{ 'title':'This is the new title' })

Now you might wonder we I haven't used readlines to read in all the lines to a list. It's basically because I want to be able to output a complete template string without any extra processing, rather than using a list of strings. I also want to avoid map and instead just use a single pass to do the replacement.

Template Principles

All of the template principles I demonstrated in the last article apply. Providing we continue to use the same templates, and follow the same basic rules about dividing and splitting the individual elements that make a page, there's no reason why using a CGI based template parser should produce any different results than when using the SSI system.

What's Next?

Page 2 of 3


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.