Need Add Your Sitemap?

Sunday, 7 October 2007

1.5.1. Server-Side Includes

The simplest mechanism for implementing a "change code in one place, change the whole site" architecture is to use server-side includes (See "Separating Content from Design," earlier in this chapter).

Most web hosting accounts provide a server-side include mechanism. You tell the web server which file extensions mean that a file can have includes. When the web server processes the file to send back to a browser for display, it looks for the special syntax that means there is an include. When it sees this syntax, it expands the page it is serving to the browser by expanding it with the file indicated by the include's syntax.

The default file extension for a web page is usually .shtml, although you can add other file extensions so that your web server will look through them for includes (there is, of course, a slight performance hit for this).

Figure 1-4 shows a pretty typical Linux and Apache web host administrative utility with the mouse cursor pointing at the button that lets you add file extensions to be parsed for includes.

Figure 1-4. You can add file extensions to be parsed for includes by the web server

For example, suppose you have a simple .shtml home page like this:

You could create two include files:

  • styles.html , which contains CSS (Cascading Style Sheet) styles for the elements used on the site such as font size and color

  • top-bar.html , which contains the site navigation bar

"You can link to an external CSS style sheet, or define your CSS styles in an include file. Either way, to change styles sitewide, you just have to change the style definitions in one file."

Now it's easy to change the appearance of the text on each page of the site by just making one change to styles.html. And if you need to change the appearance of the navigation bar, you can simply make the changes to top-bar.html, and it will be replicated across the site.

"There's generally no requirement that included files be named with any particular file extension; instead of .html you can perfectly well use .foo, or anything else you'd like."

0 Comments: