It can be very useful to create a site based on templates. Templates in Web design can allow one to separate elements used in multiple documents, and then to include them when needed at request-time. Templates require some form of [S] server-side scripting, which may be as simple as a file-include system or complex enough to run whole programs to fill in template elements.
Templating systems are simpler to implement than full-fledged script-driven sites using technologies like PHP. Templating systems require systems administrators to cede only minimal control to site developers & managers. Template-driven sites require some processing overhead as do script-driven sites, but the overhead lost can often be worth the ease of design and maintenance gained. You should consider as candidates for separation into included-on-demand files things like site-wide and/or logical-section-wide headers, standard footers, last-modified statements, and other such elements which show up on many pages. These template-elements are easy to implement and can simplify greatly the code in all the Web pages served from the server. Remember, the client never knows that templates are being used.
There are two main approaches used on most Apache servers to implement simple templating systems: XBitHack & Server-Side Includes.
The XBitHack directive shows up in both [1] Apache 1.3 and [2] Apache 2.0. It is wonderfully fine-grained and easy to control, but XBitHack requires that your files have names which end in ".html" to work. If you have files ending in ".xhtml" within which you want to embed include directives, XBitHack will not work on them. And if you change the filenames to match the requirements of XBitHack, then you run the risk of your client browsers thinking they cannot handle the MIME-type of the document returned by your server. XBitHack is probably not a useful choice to make for adding an include capability to your Website at this point in the development of the Handheld Web, although it is very widely used in the Full-Screen Web..
Server-Side [I] Includes (or SSI) are another choice for a simple yet effective templating system. While XBitHack can be turned on and off by setting the eXecute bit of the file (fine-grained), SSI is turned on for all files ending in a particular string in a particular sub-directory structure (coarser-grained). The advantage of SSI over XBitHack is that you can easily make SSI work for any file-type (like all files with names ending in ".xhtml").
The magic on Apache is simple. It just involves creating (or modifying an existing) file named .htaccess, which lives in the root of the (sub-)directory structure you wish this to work within. For example, I have a file named .htaccess on a server along with an index.xhtml file, as a [D] demonstration of using SSI. The server is running Apache 2, so the .htaccess file has the following one line in it:
AddOutputFilter INCLUDES .xhtml
As a result of that one line, any file with a name ending in ".xhtml" and living in that directory or any sub-directory under it will be examined by the server for include directives to follow before replying to a request from a client for that file. The [D] demonstration site has a file named "index.xhtml" which in turn contains the directives to include (in a specific format familiar to scripting programmers) the date that file was last modified. The directives included simply looked like this to the server:
<!--#config timefmt="Last modified: %D at %R" -->
<!--#echo var="LAST_MODIFIED"-->
Again, note that this is invisible to the client machine. Select View Source in your browser, and you will not see the code. You will just see the results of the server executing the code and inserting things into those elements as required.
There are a number of SSI directives which you can use, some of which have capabilities far beyond simple echoing things as above. These SSI directives should be used with care ("with freedom comes responsibility"), and should be used when appropriate:.
| Element | Description |
|---|---|
config |
configure output formats |
echo |
print variables |
exec |
execute external programs |
fsize |
print size of a file |
flastmod |
print last modification time of a file |
include |
include a file |
printenv |
print all available variables |
set |
set a value of a variable |
[ See [I] Includes article for details and examples ]