Apache Today [Your Apache News Source] The Definitive Source for Wireless Technology & Business
Your Daily Source for Apache News and Information  
Breaking News Preferences Contribute Triggers Link Us Search About
To internet.com

Apache HTTPD Links
Apache Module Registry
Apache XML Project
The Jakarta Project
Apache-Perl Integration Project
The Java Apache Project
Apache Project
PHP Server Side Scripting
The Apache Software Foundation
Apache-Related Projects
The Apache FAQ
ApacheCon
The Linux Channel at internet.com
Linux Today
Linux Central
Apache Today
BSD Today
Just Linux
Linux Apps
All Linux Devices
BSD Central
Enterprise Linux Today
Linux Start
Linux Planet
Linuxnewbie.org
Linux Programming
PHPBuilder
SITE DESCRIPTIONS
E-Commerce Solutions: Template-Driven Pages
Jun 28, 2000, 13 :40 UTC (14 Talkback[s]) (4123 reads) (Other stories by Martin C. Brown)

By

The basis of all web sites is HTML, and that HTML is used to implement your design. There are other elements of course, graphics, and presumably the dynamic content of your site, but the backbone of your site will be it's design and layout. Now, I'm no expert on design--I can do a technical drawing, but ask me to come up with a logo and then draw it, and I'm suddenly lost. We'll therefore ignore the design issue, and instead concentrate on the implementation!

Producing any kind of Web site, and e-commerce ones in particular, you are likely to be following a very simple layout using consistent elements like toolbars or navigation bars to help guide your users around your site. There are different ways of getting round the problems of these constant elements, frequently frames are used for this purpose, and the new CSS (Cascading Style Sheets) can be used to similar effect. I've even seen some complex sites that use JavaScript to build sections of the page dynamically.

However, for an E-Commerce site there are extra complications, most of time you'll be building sections of the page dynamically based on searches, user selections and preferences.

Before we get on to the complexities of dynamically driven sites, lets go back a step and look at a template-driven site that uses Server Side Includes (SSI) to build up the pages. Understanding the basics of SSI will help us to understand how to use and employ templates in just but about solution. We'll use my writing website, www.mcwords.com, which was actually designed with SSI specifically in mind as our example.

The home page looks like this:

The home page

Whilst the actual to build that page is short and sweet:

<HTML>
<HEAD>
<TITLE>MCwords</TITLE>
<!--#include virtual="/ssi/topbar.html" -->
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width="66%" valign=top>
<table border=0 cellspacing=0 cellpadding=2 width=100%>
<!--#include virtual="/ssi/mainnewspanel.shtml" -->
<!--#include virtual="/archive/summary.html" -->
</table>
</td>
<td width="34%" valign=top>
<table width="100%" border=0 cellspacing=0 cellpadding=2>
<!--#include virtual="/ssi/updatepanel.html" -->
<!--#include virtual="/ssi/downloadpanel.html" -->
<!--#include virtual="/ssi/dreamhostad.html" -->
</table>
</td>
</tr>
</table>
<!--#include virtual="/ssi/tagline.html" -->

Aside from some minor formatting, the bulk of the code is actually taken up by SSI include directives which import the main logo and top navigation panel, the body section, a side section and the trailing information. In essence what we've got here is a page made up of five distinct sections, the surrounding template gives us our base-the page title and main table, whilst the imported sections handle everything else.

Configuring Apache

For this to work correctly, you need to have Apache configured first to handle SSI, and then to use the index.shtml file as a default. This is fairly straightforward. To enable SSI, enable the Includes directive on a directory:

<Directory /usr/local/http/webs/mcwords>
        Options +Includes
</Directory>

Then tell Apache which files to parse for SSI commands. The general consensus is to use files with an extension of .shtml, which you configure like this:

AddType text/html .shtml
AddHandler server-parsed .shtml

Finally, you need to update the DirectoryIndex option:

DirectoryIndex index.shtml index.html

Element Design

Our sample page is actually duplicated across the entire site, I use the sample basic template and just change the include directive to include the information I want. Aside from the obvious benefits of the things like a consistent top bar, I can also use the same body elements and sidebar elements in other pages. For example the main heading page news items can be used as introductory panels for the individual sections and so on.

For E-Commerce sites you can employ the same basic tactics for search fields, control panels and even product information, although it's more likely that will be driven from a scripting language than static HTML. We'll actually look at techniques for introducing dynamic elements through our templates in moment.

The trick to handling all of this effectively is to create each included element and as a standalone component. That way the element is both standalone--in that it could be displayed essentially on its own without any further window dressing--and able to be integrated without needing any other files or window dressing to make it work.

For example, take the panel on the right; this is a single table cell, which itself has an embedded table. The "Recent Site Updates" will be a new row of that table, so the code looks like this:

<tr>
<td width=5>&nbsp;</td>
<td bgcolor="#ff0000" align=center><font face="Arial,Helvetica" color="#ffffff"><b>Recent Site Updates</b></font></td>
</tr>
<tr>
<td width=5>&nbsp;</td>
<td>
Learn more about your iMac with <a href="/projects/books/imac/index.shtml">iMac FYI</a><br>
Perl AA <a href="/downloads/paa/index.shtml">Scripts Updated</a><Br>
Get the info on <a href="/projects/books/pyaa/index.shtml">Python Annotated Archives</a><Br>
Check out <a href="/projects/books/ptcr/index.shtml">Perl: The Complete Reference</a><Br>
Download <a href="/downloads/paa/PAA04Sample.pdf">Perl AA Ch 4</a> Free! (PDF, 540k)<Br>
</td>
</tr>

That's everything--the new table row, the header for the section, and the body, as well as the trailing HTML code to close those elements off. This keeps the clutter and management of the elements out of the main files and instead inside the individual components.

Producing Your Design

The easiest way to design your site when producing a system like this is to actually create a static page--or series of pages if you are using frames--and then try to logically split that up.

Once you have the individual elements, then optimize them to keep the sizes down to a minimum (this helps your server and your prospective clients). Finally, use the final version to produce templates that can then be duplicated and used for all the different includes that you need to make your site.

How you design and layout your site is entirely up to you, and really an HTML, not Apache, issue, so I'll leave that as an exercise for the reader.

Directory Organization

Just before we look at some of the funkier things you can do with SSI and template driven pages, it's worth just covering the basics of your directory layout. On my site, I have a single directory at the top level called ssi, this contains all of the core components like the top bar, bottom tag line and the main panels on the right hand side. The main body of the content is then taken from the corresponding directories.

Although this kind of layout is not particularly unique or clever, it does make the whole process a lot easier. It also ensures that I can easily identify the elements that I want to include. This will become particularly important as we start to embed different elements through our script engines.

Quasi-Dynamic Content

Up to now, we've strictly been dealing with static pages. Although it's possible that some of your content will be static based, it's highly unlikely that your entire site will be able to make direct use of SHTML pages. We'll be looking at how to build entirely script-driven sites that employ templates in the next article. Until then, we can actually introduce some further dynamic content without resorting to a full script.

Using the exec SSI directive we can execute a CGI script instead of embedding a static HTML file. Most sites actually use this feature to include a site counter, but it can be used in any situation where you want some dynamic content. On my site I've used this as a way of introducing some random elements into the various side panels that would otherwise have to be manually selected on each page.

The script is pretty simple; it only has to select a file from a predefined list and then open and send it back. Because the script is still parsed by the server though it does need to output an appropriate header. You can see the script below:

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";

@opts = qw/paa imac beos/;

$chosen = $opts[rand(@opts)];

open(FILE,'/usr/local/http/webs/mcwords/ssi/'
          . $chosen . 'buypanel.html');
while(<FILE>)
{
    print;
}
close(FILE);

This is not a suitable solution however for anything more complex than embedding a flat script, we can't supply arguments, and the request isn't made in the same way as calling a CGI from the client. Still, we're beginning to get the basis of a dynamic template-driven environment.

The Next Step

The basics applied in this article will work with any template-driven system. In every system, from SSI to a Perl, Python or other script driven solution the aim of templates is to centralize the HTML. That makes developing and updating the site easier--you only have to change one file--and it also provides consistency across the entire site.

In the next article we'll move on and see how we can use different scripting engines and the same basic template principles. We'll also look at parsed templates, as opposed to the static templates we've seen in this article.

Martin C. Brown is a full-time writer and consultant specializing in multiplatform integration and internet technologies. He is author of both the Perl and Python Annotated Archives and Perl The Complete Reference.

  Current Newswire:
Apache Module Registration: mod_xmlrpc

ApacheCon 2001 Europe cancelled

Apache Week issue 254 (13th July 2001) released

Great Bridge PostgreSQL 7.1 package announced

ApacheCon Dublin sessions listed

Apache Week issue 253 is out

ServerWatch: June 2001 Security Space Survey Results

zez.org: Security flaws in PHP

SECURITY: Bugtraq: Java servlet cross-site scripting vulnerability

mnoGoSearch 3.1.17 released

 Talkback(s) Name  Date
  Another time saving use of inclusion....
Nice article Martin... It is certainly a neccesity in larger, content driven, websites to house your static data in template files for inclusion.

Another time saver, especially when dealing with multiple subdomains or subsites off of the main site, is to make one file to house all of the link names and urls for the main site. Therein allowing one file to be updated, while all applicable link sections can parse that one file for link data.

Example:
Create a file called main_links.txt and add the name and url of each site link.

page1 => http://urdomain.com/page1
page2 => http://urdomain.com/page2
page3 => http://urdomain.com/page3
page4 => http://urdomain.com/page4

and so on...

Now that the links themselves are static to that one file, you can use PHP, Perl, or whatever you're favorite server side scripting language is, to parse the links file and create a links menu based on the contents of the file. As long as the language has the ability to split the line data by the => delimiter,
you're all set.


  
  Jun 28, 2000, 21:08:33
   Re: Another time saving use of inclusion....
Good tip! - we'll be looking at script based templating in the next article, so look out for some other good tricks then!
--

Martin C Brown, http://www.mcwords.com   
  Jun 29, 2000, 08:48:00
  dynamic SSI
One use I had in a previous job was to develop flat templates which were then parsed by a perl script and appropriate content inserted on the fly. I got my script to look for directives like:




inside my template files and insert the appropriate content. This was rather like an ESSI system (External Server Side Include) in the way it was implemented.

This may seem wasteful but allowed me to include content from a different site in our domain (which used a database engine) using the LWP module to do fetches.

Of course all this stuff actually makes for well designed and manitainable code as well. The academic institution where I work is involved in this very area: Web Site quality engineering   
  Jun 30, 2000, 14:43:27
   Re: dynamic SSI
Dynamic SSO and using Perl to interpret SSI directives is one of the things we'll be looking at next time.

Martin C Brown, http://www.mcwords.com   
  Jul 1, 2000, 06:32:10
   Re: dynamic SSI
Unfortunatly, the SSI code you mentioned was left out in the post. Without using the Html code for you're (& l t ;) < and (& g t ;) > characters,
we can not see the SSI...   
  Jul 2, 2000, 08:16:07
  Too wide on the screen!
This article is very hard to read because it's too wide to fit on my computer screen. The required horizontal scrolling is extremely tedious, distracting, and a big no-no!
  
  Jul 3, 2000, 20:02:39
  the exec directive
We've been using SSI includes on our static pages for a couple of years now, and it has made organization (and redesigning) of the site much easier.

But as far as using exec, I was warned off it, told "the overhead of doing an exec is grossly higher (sometimes several 1000s of times higher) than the overhead of doing a simple include" by a programmer/engineer who worked for our hosting company.

Also, does using SSI's exec present security problems?

  
  Jul 5, 2000, 22:31:31
  Informative Page
Hi,

A very good and informative page. I would like to see a bit more on how to write CGI Scripts that can help on doing more interactive and user friendly sites.

Good Luck,
Meysam   
  Jul 6, 2000, 15:20:44
   Re: Informative Page
> I would like to see a bit more on how to write CGI Scripts that can help on
> doing more interactive and user friendly sites.

What is your language preference for CGI scripting?
Perl, PHP, TCL, etc...?
  
  Jul 7, 2000, 10:23:43
   Re: Re: dynamic SSI
Works here - I did use >/< for all the embedded HTML code.

What browser are you using?

Martin C Brown, www.mcwords.com   
  Jul 9, 2000, 06:28:39
   Re: the exec directive
There is an implied overhead for running an exec, since it requires the server to load ane execute a CGI for each exec directive. This is far less efficient than using a single script to do all of the template work for you, especially if you decide to incorporate a number of exec directives into the same page.

I certainly wouldn't recommend it for a very busy site, but for short alternative includes like the ones mentioned in the article it should be OK - just don't use too many.

As for the security, there aren't any real worries. An end user couldn't change the SSI directives (Since they are part of the HTML stored on the server), and the CGI scripts are run as requests *from* the server, not from the client, so there's no danger of any extraneous data being sent.

In fact, from reading the HTML returned by the server to the client, you wouldn't even be able to tell how the particular element was included, as the the SSI references are removed during the parsing process by the server.

Of course, it's possible that a malicious use could come in and modify the script and/or HTML, but if they've got that far, you've got a whole bigger problem!

Martin C Brown, www.mcwords.com   
  Jul 9, 2000, 06:35:32
   Re: Informative Page
That's really the point of the whole series, albeit with an E-Commerce focus - keep reading!

Martin C Brown, www.mcwords.com   
  Jul 9, 2000, 06:36:37
   Re: Re: Informative Page
I don't really have a preference - I just use the language that seems appropriate, and that will depend largely on the project. I've used Perl, Python, C, shellscript, PHP, Gawk, server-side Java and many other things in between.

If the whole thing is a Python project, then I tend to stick with that for everything, just because it makes the interaction between scripts and backends easier.

Martin C Brown, www.mcwords.com   
  Jul 9, 2000, 06:39:23
  Bad page layout
My computer at home is only 800x600, due to a monitor that can't handle any more, and it's very irritating that I should have to constantly scroll left and right because the text won't fix within 800 pixels across.

*sigh*   
  Sep 25, 2000, 14:16:50
Enter your comments below.
Your Name: Your Email Address:


Subject: CC: [will also send this talkback to an E-Mail address]
Comments:

See our talkback-policy for or guidelines on talkback content.