GuidesApache Guide: Advanced SSI Techniques

Apache Guide: Advanced SSI Techniques

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This is the third in a three-article series about Server Side Includes, or
SSI. In the first article, I discussed configuring your Apache server to permit
SSI. In the second article, I gave some examples of how you might use SSI to
add dynamic content to your web pages. In this article, I’ll talk about some of
the advanced features available with Server Side Includes.

In the third part of his series on Server Side Includes, or, SSI, Rich Bowen covers some advanced SSI techniques, including setting variables, expressing conditionals.

(Note: Most of the features discussed in this article are only available to
you if you are running Apache 1.2 or later. Of course, if you are not running
Apache 1.2 or later, you need to upgrade immediately, if not sooner. Go on. Do
it now. We’ll wait.)

Setting Variables

Using the set attribute, you can set variables for later use.
We’ll need this later in the discussion, so we’ll talk about this here. The
syntax of this is as follows:


In addition to merely setting values literally like that, you can use any
other variable, including, for example, environment variables, or some of the
variables we discussed in the last article (like LAST_MODIFIED,
for example) to give values to your variables. You will specify that something
is a variable, rather than a literal string, by using the dollar sign ($)
before the name of the variable:


To put a literal dollar sign into the value of your variable, you need to
escape the dollar sign with a backslash:


Finally, if you want to put a variable in the midst of a longer string, and
there’s a chance that the name of the variable will run up against some other
characters, and thus be confused with those characters, you can place the name
of the variable in braces, to remove this confusion. (It’s hard to come up with
a really good example of this, but hopefully you’ll get the point.) To wit:


Conditional Expressions

Now that we have variables and are able to set and compare their values, we
can use them to express conditionals. This lets SSI be a tiny programming
languages of sorts. mod_include provides an if,
elif, else, endif structure for building
conditional statements. This allows you to effectively generate multiple
logical pages out of one actual page.

The structure of this conditional construct is:


A test_condition can be any sort of logical comparison--either
comparing values to one another, or testing the "truth" of a
particular value. (A given string is true if it is nonempty.) For a full list
of the comparison operators available to you, see the mod_include
documentation. Here are some examples of how one might use this construct.

In your configuration file, you could put the following line:

        BrowserMatchNoCase macintosh Mac
        BrowserMatchNoCase MSIE InternetExplorer

This will set environment variables Mac and InternetExplorer
to true, if the client is running Internet Explorer on a Macintosh.

Then, in your SSI-enabled document, you might do the following:

        Apologetic text goes here
        Cool JavaScript code goes here

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories