In our last exciting episode, I presented a very basic use for SSI. Let's take a closer look. Here's an example include directive which pulls in the contents of a file named content.html located in my www directory.

HTML:
  1. <!--#include virtual="/content.html" -->

Now that's pretty powerful if you have never seen it before, but it gets old quick. Let's add a little power using an Apache variable.

HTML:
  1. <!--#set var="content" value="/content.html" -->
  2. <!--#include virtual="$content" -->

That still doesn't seem very powerful, but there is nothing that says those two statements have to be in the same file. I'll define the content variable in one file and use it in an included file.

HTML:
  1. <!-- filename: index.shtml -->
  2. <!--#set var="content" value="/content.html" -->
  3. <!--#include virtual="/load_page.shtml" -->

HTML:
  1. <!-- filename: load_page.shtml -->
  2. <!--#include virtual="$content" -->

One advantage here is that we can have a lot of those index files. One for each page on the web site. And one of the content files for each page. In my experience I tend to create the index file once and then forget about it and the load_page file. Most of my updates are to the content files.

Now, I'd like to introduce you to the conditional (if-then-else) directive and the echo directive. Here's an excerpt of a larger page loading file.

HTML:
  1. <!--#if expr="$title" -->
  2.     <!--#echo var="title" -->
  3. <!--#else -->
  4.     Default Title
  5. <!--#endif -->
  6. </title>

In this excerpt, if the title variable has been defined it will be used, otherwise "Default Title" will be used. The true power of this is in that the index files do not have to define every variable used in the page loading file.

I have found that using these defaults on every variable in the page loading file makes that file unreadable. It just has way too much stuff in it with all those default values. So, I define a default_values.html file.

There's a another subtle problem with that solution. Apache does not allow us to call a file which sets variables for that file. The file which set the variables must be the one which does the calling. It is possible, however, for the index file to load the default values file and then for the default values file to call the page loading file.

This is getting a little complicated. Let's look at a crude diagram. I need to make that up and I need to go out to get groceries and something for supper. So I'll present the diagram and (hopefully) clear up some confusion in the next exciting episode.

Digg!