Recipe 4.4. Including Dynamic Content in Static Pages


Problem

You need to include dynamic content in the body of a static HTML page.

Solution

Use your server's built-in SSI parsing of <!--#include -->, <!--#echo -->, and <!--#exec --> tags in the body of your static pages.

For example, server-side include tags can print the current date and time, or a file's last modification date on an HTML page. Here is the tag for displaying the date:

 <!--#echo var="DATE_LOCAL" --> 

With this tag, you'll get the server's default date format, which might look something like this:

 10-May-2005 16:09:53 CDT 

Discussion

In Recipe 1.4, I explained how, by saving small files containing text and HTML code in one location on your server, you can embed their contents on other pages throughout your web site by placing simple server-side include tags in your web page code. Using SSIs for common, frequently updated content facilitates fast site updates, since you only have to update one file for the change to take effect everywhere the SSI appears on the site. With SSIs, though, you can embed much more than included text in otherwise static web pages.

Server-side includes are great for small, time-saving tasks, but I'm of the opinion that date stamps on web pages are redundant, since anyone at a computer should have ready access to a clock and calendar. But that doesn't mean date tags are useless. Automating the display of the current month or year can be a real time-saver when used in the small, easily forgotten corners of your site. For example, add an SSI tag to display the current year in the copyright notice for your site, and you'll never have to remember to update that bit of web page code by hand again. Likewise, if you have links throughout your site to a regularly updated calendar of events, use the SSI date code for the month to keep that link current.

An SSI also can print the last modification date for a web page. The variable uses the same formatting codes as the DATE_LOCAL variable. The code to print the variable looks like this:

 <!--#echo var="LAST_MODIFIED" --> 

More so than date stamps, modification dates have great value for web surfers. Knowing when a page was last changed augments their impression of a site's legitimacy and reliability.

If you can crow about how often you update pages on your web site, add the LAST_MODIFIED variable to your pages. Leave it out if you can't.


You can tweak the format with another SSI tag placed above the command to echo a date, like this.

 <!--#config timefmt="%D" --> 

Table 4-2 shows a handful of the many date format codes that can be included in this tag. Others are available for displaying the time down to the second or the time zone. Codes can be combined, so that this:

 <!--#config timefmt="%B %d, %Y" --> <!--#echo var="DATE_LOCAL" --> 

yields this:

 May 10, 2005. 

Table 4-2. A selection of date formats for use with server-side includes.

Date format code

Displays

A

Day of the week, i.e., Tuesday

B

Month name, i.e., May

D

Numerical date as month/day/year, i.e., 05/10/05

F

Numerical date as year-month-day, i.e., 2005-05-10

Y

Just the four-digit year

d

Just the day of the month, i.e., 10


You also can use SSIs to embed the results of a Perl, PHP, shell, or other script in a web page. Everything from simple hit counters to the results of complex database queries can be placed in a static HTML file with the <!--#virtual --> or <!--#exec --> SSI tags.

Include a simple script with a tag like this:

 <!--#include virtual="/cgi-bin/hello.cgi"--> 

or this:

 <!--#include virtual="/includes/nav/script.php" --> 

The SSI tag also can pass arguments to a script. Say, for example, you have a script that returns a list of items from your online catalog based on criteria you specify. This hypothetical include tag would query the catalog for five sale items, which could then be displayed on an otherwise static page:

 <!--#include virtual="/cgi-bin/catalog.cgi?items=sale&show=5" --> 

See Also

The Apache Software Foundation has a server-side include introduction and tutorial at http://httpd.apache.org/docs/howto/ssi.html.



Web Site Cookbook.
Web Site Cookbook: Solutions & Examples for Building and Administering Your Web Site (Cookbooks (OReilly))
ISBN: 0596101090
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Doug Addison

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net