Using Server Side Includes to include content instead of layout

Using Server Side Includes to include content instead of layout

Server Side Includes are usually used to include another text file in a web page. The content is put in the main file and the layout such as the heading and menu are included using Server Side Includes. Here is an example:
<html>

<head>
<title>My SSI page</title>
</head>

<body>
<!--#include file="heading.html"-->
<!--#include file="menu.html"-->
This is where the content goes
</body>

</html>

What if you create 100 pages like this and after that you discover that you forgot to include a copyright message under the content? You will have to add it to all 100 pages which defeats the purpose of SSI.
What you really need to be able to do is keep all your content in seperate files and have one index file with the layout in it like this:
<html>

<head>
<title>My SSI page</title>
</head>

<body>
<h1>This is the heading</h1>
<a href="index.html">Home</a>
<a href="page1.html">Page 1</a>
<a href="page2.html">Page 2</a>
<!--Include the content here-->
</body>

</html>

If you want to do it this way then you must first create all the files that hold the content. The files must have only content and not things like head and body tags but may have p tags and things like that.
Next create index.shtml which will be the main page with the layout in it. You can use the above example code in the new index.shtml.
You now need to change the links so that they have a querystring in them. A querysting is the part after the ? in a URL.
<a href="index.shtml?main.html">Home</a>
<a href="index.shtml?page1.html">Page 1</a>
<a href="index.shtml?page2.html">Page 2</a>

Instead of putting the filename in the line that includes it we will put the $QUERY_STRING variable. The querystring will be a name of a file so the file will be included.
<!--#include file=$QUERY_STRING-->
But there is one last problem. When the index page is loaded on the first visit to a site it doesn't have a querystring. All we need to do is use a simple if-then-else to fix the problem. Here is the final page:
<html>

<head>
<title>My SSI page</title>
</head>

<body>
<h1>This is the heading</h1>
<a href="index.html">Home</a>
<a href="page1.html">Page 1</a>
<a href="page2.html">Page 2</a>
<!--#if expr=$QUERY_STRING-->
<!--#include file=$QUERY_STRING-->
<!--#else-->
<!--#include file="main.html"-->
</body>

</html>



Written by