FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Architecture & Design
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2002
Template-Based Web Sites as Easy as P-H-P

Jason Gilmore

WebReview.com: Template-Based Web Sites as Easy as P-H-P

Rank: 3

Create Your Own PHP-Driven Site

In this article we'll create an online database of movie entries to practice incorporating PHP templates into our web sites.

PHP makes our lives easier. We began our exploration into the good life with What You Should Know About PHP. In that article I focused mainly on PHP concepts with a little script thrown in for fun. Now we'll delve deeper into the power of PHP and cover several strategies that will help you quickly set up a site that's easy to maintain and update.

To keep things interesting, we'll create a hypothetical movie database to serve as our example. This database will have an introductory screen displaying a list of movies. Each list item will be PHP-powered so that a simple click on any title will reveal detailed information about the movie. What's beautiful about this example is that the movie information is actually being inserted into a template that acts in part to preserve a uniform environment, and in part to allow the developer to quickly modify the look and feel of the site when necessary. Let's begin by listing the files that we will use throughout the example:

header.inc

header.inc will provide the head information for the page being served. This includes the opening HTML tags, the page title, and BODY information. The extension '.inc' is used to specify files that are included within our template. This is also the file in which you could designate global variables and create PHP functions that will be used later on.

<?
// file: header.inc
// purpose: supplies head information to documents making up our site.
// created: February 11, 2000
?>

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>

<BODY BACKGROUND="" BGCOLOR="#ffffff" TEXT="#000000" 
 LINK="#808000" VLINK="#000000" ALINK="#606060">

footer.inc

footer.inc will provide the end information for the page being served. This includes the copyright information and closing HTML tags.

<?
// file: footer.inc
// purpose: supplies footer information to documents making up our site.
// created: February 11, 2000
?>
Copyright © 2000 | <A href = 'mailto:derrick@webreview.com'>Contact us</A> 
                 | <A href = 'template.php3'>Home</A>

</BODY>
</HTML>

Our sample movie entries

We'll use these three sample movie entries to practice with our PHP script:

Scarface.inc
Movie Name: Scarface<BR>
Year: 1983<BR>
Starring: Al Pacino, Michelle Pfeiffer, Mary Elizabeth Mastrantonio<BR>
Summary: The rise of Cuban immigrant Tony Montana.
Staying_Alive.inc
Movie Name: Staying Alive<BR>
Year: 1983<BR>
Starring: John Travolta<BR>
Summary: Dancer Tony Manero tries to make it on Broadway.
Saturday_Night_Fever.inc
Movie Name: Saturday Night Fever<BR>
Year: 1977<BR>
Starring: John Travolta<BR>
Summary: Teenager Tony Manero struts his stuff on the dance floor.

template.php3

template.php3 acts as the brains of our site. As you will see, we will pass variables into this script through hyperlinks, controlling the information that will be viewed by the reader. This template is a very basic example, but will more than suffice to show the general mechanics of template-driven sites.

<?
// file: template.php3
// purpose: central brains for the site
// created: February 11, 2000

// include header information

INCLUDE "header.inc"; 

print "
<table width = 100% border = 0>
 <tr>";

 // The following cell will hold the move list for the site.

 print "<td valign=top>
  Choose your movie:<BR>
  <UL>
  <LI><A HREF =
'template.php3?topic=Saturday_Night_Fever'>Saturday Night
Fever</A>
  <LI><A HREF = 'template.php3?topic=Staying_Alive'>Staying
Alive</A>
  <LI><A HREF =
'template.php3?topic=Scarface'>Scarface</A>
  </UL>
 </td>";

 // The following cell will include the information requested by the
user.

 print "<td valign = top>";

  // If no topic selected, display the default content.

  IF ($topic == '') :

   print "Welcome to WebReview's movie database."; 

  // Else the user clicked on a link.
  // Include content related to that link.

  ELSE :

   INCLUDE "$topic.inc";

  ENDIF;

 print "</td></tr></table>";

// Include footer information

INCLUDE "footer.inc";

?>

Script Tip

Another alternative to using links could be a drop-down list. Simply give the drop-down the name 'topic', add a 'submit' button, and Voil!

The comments within the template.php3 script should be rather self-explanatory of the actions the script will take, but it may be worthwhile to highlight a few points:

  • Header.inc and footer.inc will be included in every page, no matter what the user is trying to view.
  • Note that the variable '$topic' controls the action of the script. When topic is empty, template.php3 will display the specified default information. In our example, this default information would be 'Welcome to Web Review's movie database.' However, this could be an index page, some announcements, or whatever you feel necessary.
  • When the user clicks on one of the movies, the variable '$topic' is given the value of whatever movie has been clicked on. The script, in effect, calls itself, supplying the variable '$topic' with the chosen value.
  • The '$topic.inc' file included when the user chooses a movie will be determined according to the movie that the user clicks on. For example, if one were to click on Saturday Night Fever, the file subsequently included would be 'Saturday_Night_Fever.inc'.

Even easier...

Script Tip

Those users with the Windows version of PHP may have to make minor changes to the following script in order to suit Window's directory structure. Please refer to the PHP manual, as this task is beyond the scope of this article.
Templates seem like the way to go, don't they? And I think this is true even if you are developing a small site. On the other hand, what if you were creating a particularly large site with thousands of movies? You'd probably be too busy creating movie reviews to spend time modifying the template.php3 every time you made a change. Well, hold on to your hats, PHP fans, because I'm about to show you a trick that I think you'll really like:

template.php3 (revised)

To make this work correctly, place the following code in between the <UL>...</UL> statements in the original template.php3 file.

// Open a link to the current directory.

  $dir_handle = opendir('.');

 // While still files in directory left to look at

  while ($file = readdir($dir_handle)) :

 // Make sure the file is a movie review

 if ($file != "." && $file != ".." && 
    $file != "header.inc" && 
    $file != "footer.inc" &&
    $file != "template.php3") :

 // Replace each underscore ('_') with a blank space.

  $name = ereg_replace("_", " ", $file);

 // Strip off the .inc from the file name.

  $name = ereg_replace(".inc", "", $name);

 // Strip off the .inc from the file.

  $file = ereg_replace(".inc", "", $file); 

 // Print link and file name.

  print "<LI>
  <A HREF = 'template.php3?topic=$file'>$name</A>";

 endif;

endwhile;

// close the directory

closedir($dir_handle);


This script may seem a little intense at first, but it's really quite straightforward. Let's break it down step-by-step:

  1. Open a link to the current directory. (opendir)
  2. While we still have files to review... (The while statement)
  3. Make sure that the current file is a movie review. (The if statement)
  4. Using variable '$name', strip off the '.inc' and replace the underscore '_' with a blank space. This will be the name that the user will click on. We want the link to be user-friendly, therefore it is best to display the name in the way the user would expect it. (ereg_replace)
  5. Strip the '.inc' from the file_name that will be placed within the link. (ereg_replace)
  6. Print out the link pointing toward the movie review. (print statement)
  7. Close the file handle. This is good programming practice!

Again, the beauty of this script is that any addition/deletion of movie reviews is automatically recognized by the script the next time it is loaded to the browser. This prevents the possibility of broken links, thereby freeing you to concentrate on content for the site!

I hope you have time to play with these scripts, as doing actual coding is the best way to learn a language quickly. Furthermore, the examples provided within this article are just one way to accomplish this task. It is important to remember that PHP is very flexible; there are almost always several ways to accomplish the same thing, so experiment, read the manual, and above all, have fun doing it!

RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK