The Lab Book Pages

An online collection of electronics information

http://www.labbookpages.co.uk

Dr. Andrew Greensted
Last modified: 18th November 2009

Hide Menu


Valid XHTML Valid CSS
Valid RSS VIM Powered
RSS Feed Icon

This site uses Google Analytics to track visits. Privacy Statement

Page Icon

SSI - Server Side Includes

There are loads of tutorials, FAQs and other bits 'n' bobs on SSI. Even so, finding the useful bits can be a pain. So to add to the plethora...


File Inclusion

OK so the most obvious use of SSI is to add a file into the document being served. This is massively useful when creating an easily updated web structure. Create a head and foot file, containing page layout and common stuff like menus. Then, for each content page, just include them. File paths are relative to the current page.

<!--#include virtual="../common/html/head.shtml" -->

<p>There are loads of tutorials, FAQs and other bits 'n' bobs on SSI. Even so,
finding the useful bits can be a pain. So to add to the plethora...</p>

<!--#include virtual="../common/html/foot.html" -->

Variables

Variables can be used to ease the update of common information, or to pass information to included files. For example, if you want to include a 'location bar' on each page, but want the HTML that displays it in your common head (or foot) page, then you can pass the location text using a variable.

<!--#set var="LOCATION" value="Web -> SSI" -->
<!--#include virtual="../common/html/head.shtml" -->

<p>This is my page content.</p>

Then in head.shtml page use this to include the variable. Note that definition of the variable had to be done before the inclusion of the head page.

<span class="location"><!--#echo encoding="none" var="LOCATION" --></span>

Test for Set State of Variables

It can be useful to test if a variable has been defined. In the example of using a variable for a page location bar, what happens when the echo tries to display an undefined variable? It displays a rather annoying (none).

SSI has an if command that can be used for this very thing. Try changing the name of the defined variable and see the difference.

<!--#set var="LOCATION" value="Somewhere in the website" -->

<!-- Without a test. Will display (none) if not set -->
<p>Location: <!--#echo encoding="none" var="LOCATION" --></p>

<!-- With a test, much safer/tidier -->
<!--#if expr="${LOCATION}"-->
   <p>Location: <!--#echo encoding="none" var="LOCATION"--></p>
<!--#endif -->

File Modification Date

It's handy for web page readers to get an idea of when a page was last modified. Especially if it contains technical information. A simple way to achieve this is to use the LAST_MODIFIED variable.

The snippet below shows how the format of the date may be set, then the date itself printed.

<!--#config timefmt="%d %B %Y" -->
<!--#echo var="LAST_MODIFIED" -->

Book Logo