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

First of all, I feel I should apologize to those readers who've been awaiting the follow-up to my original article. Unfortunately, pressures of work on other projects has meant that I've been unable to switch my attention to any other projects, including the E-Commerce series. However, I'm now back on track, so here we go with the follow-up on template-driven pages, this time using Perl and Python to deliver those SSI templates straight into CGI documents.

If you're not working with templates, you should be -- templates are an easy way to impose order on sometimes-sprawling Web sites. In this article, Martin C. Brown will describe how to use Perl and Python to deliver SSI templates straight into CGI documents. straight into CGI documents.

In the last article we examined the different ways in which we could introduce pages purely using the SSI (Server Side Includes) features of Apache. The problem with SSI is that it doesn't apply to pages that have been served by a CGI script. Instead, you need to manually read and regurgitate the template for yourself within the script or CGI service that you are using.

You don't actually need to use a different template, you can continue to use the SSI templates that we've seen before, as long as you keep to the same rules as you would with a server-parsed HTML file.

The obvious way of outputting an HTML file is just to open the file, slurp in the text, and print it out. But, if we're going to go to all this trouble, why don't we do a little more than just regurgitate the text?

Using Perl

Perl is ideally suited to reading in entire files, doing a bit of processing, and then print it out again. I have for years now used the same basic function of reproducing templates in Perl. The function looks like this:

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

        or print "I tried to load <br>\n";
        local $/;
        sh = <TEMPLATE>;

    foreach  (sort keys %subs)
        sh =~ s/\%\%\%\%/{}/g;
    return sh;

There are a few points to note about this function before we look at how best to use it. First and foremost, you'll notice that we load the entire template file into memory. This is because we want to process the file in it's entirety. The second point is that we don't actually print the template from within the function, instead, we return the translated text to the caller. This is just in case we want to use the template for something other than an active HTML page generated by a CGI script. We could use the same function to introduce templates into a static HTML file, whilst still allowing us to reproduce and parse the template in the process.

The third point is just a small nicety. If the file that's been selected doesn't exist, we print a little message to say that there's been an error. We could equally return nothing, but I prefer to be able to spot the problem. In production systems, I've actually used an SSI type error message, and also taken the time to mail an error message to the webmaster to highlight a possible problem.

Now for the important part. The second half of the function actually processes the template so that we can embed elements into the templates that can be replaced on the fly. We replace strings of the form %%string%% by using a hash which we supply to the function. The key of the hash is the string, and the value is the replacement text. For example, take the simple template:


Using the function above we can print out the template using:

print parse_template('template','title' => 'This is the title text');

This will produce the desired:

<title>This is the title text</title>

This article was originally published on Sep 13, 2000
Page 1 of 3

Thanks for your registration, follow us on our social networks to keep up-to-date