Including Content - A Review
Part
II - Using SSI, PHP and ASP Includes
by Katherine Nolan
There are a number of reasons why you might need to use includes other
than the FrontPage ones. The most important is probably in the case of sites which will be on a
server without extensions, but also quite often scripts will require you to include content on pages
and this cannot be done with FP includes.
Server Side Includes (SSI)
The most commonly used alternative is using Server Side Includes
(SSI). Setting these up is just the same as if you were using FP includes with a few minor differences.
Many of these differences will also apply to other types of include which we will look at later,
so I will go into some detail on them here.
First though, lets take a look at the code required to include content
using SSI. This is what the SSI include directive looks like:
<!--#include file="page.txt"
-->
This will work only if the file to be included is in the same
directory as the final page, so, given that will usually not be the case, the more commonly used directive
is:
<!--#include
virtual="/directory/page.txt" -->
In this case it is very important that you get the path to your file
correct.
Some Important Points
1. Naming Your Final Pages
Files which use SSI includes need to be named with an extension that
alerts the server to check for include directives before serving the page. The most commonly used
one is .shtml, but .shtm is also used. Most servers will not parse .htm or .html files for SSI but
they can be configured to do so. Generally speaking this is only practical if you have a dedicated
server since configuring the server to parse .html files would cause all standard HTML files
served to be first parsed (or checked) for SSI, a situation which would cause some strain on the
server.
2. SSI and Page Load Speed
There is no doubt that using includes, of any sort, causes some increase
in the page load time. In most cases this will not be a
major concern and will be of a small order. There is of course a speed
advantage once one page on a site has been loaded as after that the include pages will be cached and
subsequent pages will load quickly.
How you write your includes and the format in which you save them has an
effect on the initial load time as you will see below.
3. Naming Files
Notice that, as the file names in the directives above indicate, the
include files do not have to be HTML files, you can include .txt and other files. It may be more
convenient to leave the files with a .htm extension as this means they can be easily opened and edited
in FP or whatever editor you are using. On the other hand .txt files are smaller, handled more
rapidly by the server and thus may be a better choice.
You can easily save a separate .htm and .txt version of each include.
Then if you need to edit the include you can open the .htm version, make your changes, save it and
then save it in the same name but with a .txt extenion. That way you can have the convenience of one
file format with the efficiency of the other.
4. Include Page Content
Whether you include a .htm or a .txt file you need to remove everything
from the page except the content that is to be included. That is to say you should remove the
<html></html> tags, the <head></head> tags and everything between them, and the
<body></body> tags.
While when you use FP includes these are removed for you by FP this is
not the case with SSI. If you leave them in they will all appear in the final page, resulting in
really messy HTML that is difficult for a browser to handle.
Troubleshooting
If you are trying SSI for the first time you may have some initial
problems, this is a quick look at solving some of the most common ones.
Problem 1
Where the included content should be you see instead the following
[An error has occurred while processing this directive]
This means that the server cannot find the file that is to be included.
Assuming that you have actually uploaded the file, check your include directive carefully:
<!--#include virtual="/directory/page.txt" -->
Is the path to the file correct?
Are all the directory names and the file name correct?
Is the file on the server in that directory?
Problem 2
The include file appears but all the image links are broken in it.
When an FP file is included using FP includes, FrontPage takes care of
ensuring that the paths to any images or links which are in the included page remain valid when the
file is included, no matter where in the directory structure the final file is located. This is
not the case with SSI. So, for example, if you have a header include file in the 'includes'
directory and you include it in a page in the main directory it will go
in precisely as written. That means that all links and image paths in it will be incorrect.
There are a few ways around this. One is using only absolute links and
paths in the include page, another is creating
separate includes for each level in your directory structure. The best
way though is to make sure that all paths in your include file start with a '/' and from there give
the full path from the site URL. That is to call an image which is here:
http://www.yoursite.com/images/moreimages/image.gif
you would use the following as your image tag:
<img scr="/images/moreimages/image.gif">
This is NOT the way FP will write the path by default so you may need to
edit the HTML of your include file to use this format.
PHP Includes
If you are on a Unix server then PHP includes may be a useful alternative
to consider.
The syntax for a PHP include is:
<? include('/directory/file.php'); ?>
Again all the points about the path to the file mentioned for SSI above
will apply. Also note the following in relation to
PHP:
- You can include files with .php, .html, .txt and other extension.
- The final page must have a .php extension.
- Again, you should strip out everything except the actual content you wish to include.
One of the big advantages of PHP includes is that you can include content
from a different URL, which is not possible with any of the include methods we have looked at up
to now. To do this you use the directive:
<? include('http://www.someothersite.com/directory/file.php');
?>
This can be very convenient if you wish to share content
across sites.
ASP Includes
The syntax for including files on an ASP page is identical to that for
SSI above:
<!--#include file="page.asp" -->
Or in the case of a file in another directory:
<!--#include virtual="/directory/page.asp" -->
- You can include files with .asp, .html, .txt and other extension.
- The final page must have a .asp extension if code in the included page is to run
correctly.
- Again, you should strip out everything except the actual content you wish to include.
So there you are, an include method for any situation!
Next week, in the final part of
this tutorial, we will look at using includes and
templates to speed up your development time and
build easily maintained sites.
<<
Part I - Introduction to Include Pages
>> Part III - Creating Templates with Includes
Katherine Nolan
OutFront.net
A Microsoft FrontPage Learning Community
|