IT581: Practicum in Web Development, Summer 2015 (Hybrid)

Server Side Includes

SSI implementations


File naming conventions


SSI command syntax

A SSI command is written in the form:

       <!--#command attribute="value" attribute="value" ... -->

SSI commands

echo
print variables
include
include a file
fsize
print size of a file
flastmod
print last modification time of a file
config
configure output formats
printenv
print all available variables
exec
execute external programs
set
set a value of a variable
 
(Links are to Apache manual entries)
 

Standard variables

<!--#echo var="DATE_LOCAL" --> 
Tuesday, 19-Nov-2024 02:30:58 EST

file and virtual attributes.


The #include element

Example 1

   <head>
       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
       <title>Demonstration of Server Side Includes</title>
   </head>

   <body>
   <h1>Demo of Server Side Includes</h1>
   
   <h2>Some of the commands</h2>
   
   <p>This file name: <!--#echo var="DOCUMENT_NAME" --></p>

   <p>Local date: <!--#echo var="DATE_LOCAL" --></p>
   <p>GMT date: <!--#echo var="DATE_GMT" --></p>
   <p>This file last modified: <!--#echo var="LAST_MODIFIED" --> </p>

   <p>This file's size: <!--#fsize file="ssi_example1.shtml" --> </p>
   <p>Other file last modified: <!--#flastmod virtual="plugin.txt" --> </p>
   <p>Other file's size: <!--#fsize file="plugin.txt" --></p>

   
   <h2>An included file</h2>
   <p>The following lines are contained in the file plugin.txt.</p>
   <!--#include virtual="plugin.txt" -->

   <h2>Execute a CGI script:  </h2> 
   <p>The script is in a separate .cgi file</p>   
   <div style="white-space: pre;">

   <!--#exec cgi="script1.cgi" -->
   </div>


   
   </body>
   </html>

Directory containing example files. The example is saved with three different extensions - .shtml, .html, and .txt. Only the .shtml one has the commands executed.

Example 2

If an included file also has the extension .shtml, commands it it will be executed too.

   <body>
   <h1>Demo of Included Server Side Includes</h1>
   
   <p>The following content comes from executing commands stored in the file more.shtml.</p>

   
   <!--#include virtual="more.shtml" -->
   
   </body>

The included file more.shtml:

    <p>Echoed values are for the requested document, which is not necessarily the one containing the command.</p>
  
   Its name is <!--#echo var="DOCUMENT_NAME" --> <br />
   It was last modified <!--#echo var="LAST_MODIFIED" --> </p>

Directory containing the example files.



Building sites with SSI

The common components of the pages, such as headings, navigation menus, and footers, are saved separately and included in each page.

Example (uses SSI)

File main.shtml

<!--#include virtual="head.txt" -->

    <title>More SSI: Main page</title>
    <!-- Custom style -->
    <style type="text/css">
        h1 {background: yellow; color:red;}
    </style>

<!--#include virtual="nav.txt" -->

<h1>Welcome to SSI demo</h1> 


<p>This is the home page</p>

<!--#include virtual="foot.txt" -->

File second.shtml

<!--#include virtual="head.txt" -->

    <title>More SSI: Second page</title>
    <!-- Custom style -->
    <style type="text/css">

        h1 {background: cyan; color:maroon;}
    </style>

<!--#include virtual="nav.txt" -->

<h1>Continued SSI demo</h1> 

<p>This is the second page</p>

<!--#include virtual="foot.txt" -->

File head.txt

<!DOCTYPE html>
<htmllang="en">

<head>
 <meta charset ="utf-8">

File nav.txt


</head>
<body>

<hr>
<pre><a href="main.shtml">HOME PAGE</a> |  <a href="second.shtml">SECOND PAGE</a></pre>

<hr>
<hr>

File foot.txt

<hr />
<p>Foot Foot Foot Foot Foot </p>

</body>
</html>

Directory containing the files for example 3.


Advantage

Supports modularization - pages can have several standard components, such as headers, footers & sidebar menus, and each page can include customized versions.

Disadvantage

Although there is only one copy of each included file, the #include statements are repeated many times, which could lead to errors (such as typographical errors) and inefficiencies (for example, if the name of an included file is changed, all the statements will have to be changed).

Comments to: dong@hood.edu
Last Modified: 31 July 2015. 17:57