Details of producing a Template based site with PHP and HTML

Web Programming 1

A common requirement when designing a site is the ability to update the layout and HTML of the site without having to change the look and feel of every page on your web site. Template based web sites allow you to produce dynamic web content and give your visitors the appearance that each page on your site is different. Another advantage of a template based web site is that PHP and HTML code can be kept entirely separate, making for far more logical HTML and PHP coding. There are a number of ways to do this, and this tutorial covers just one method with 2 storage options: templates in a database and templates in a directory.

The first example uses a number of templates stored in either a MySQL database or a directory. Each template contains static HTML content, as well as 'variable' tags that will be replaced with data by your PHP script. Lets take a look at one possible template:

Example template 1 <html>
<head>
<title>$pageTitle</title>
</head>
<body>
<h1>$pageTitle</h1>
<p>$pageText</p>
</body>
</html>

As you can see, the template is made up of standard HTMl elements, but also includes PHP variables, such as $pagetitle. These will eventually be replaced with values specified in our script. First however we'll need to create a function to read data according to certain criteria. We'll create one that uses a database method or a file and directory method. You can use either provided you have a database on www3.mfaca.sva.edu (see Levent for details).

Example getting data: a PHP function

This function tries to find a document that will fill the template, and if it doesn't, it will try to use a database. This is done to give a sense of the two techniques. For the time being, using files is easier.

function getPage($name){
  // A global folder variable (example $dataDirectory = "/home1/andyd/site1Data")
  // If it's not set, the flow drops down to use the database instead.
   global $dataDirectory;

  if(file_exists("$dataDirectory/$name")){
    $page = file_get_contents("$dataDirectory/$name");
    return $page;
  }

 We won't get here unless the file was not found.
  #retrieve from database
  $query = mysql_query("SELECT content FROM pages WHERE name='$name'");
   if( mysql_num_rows($query)!=0) {
     #get the data from the results of the search
     $page = mysql_result($query,"content");
     return $page;
   }else return "<html><body>Page not found: $name</body></html>";
}

Please note that the previous code assumes that you have already established a connection to the database and selected the database using mysql_connect and mysql_select_db. The function also requires a table in the database called pages, created using the following MySQL statement:

CREATE TABLE pages (
 id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
 name varchar(30) NOT NULL,
 title varchar(30) NOT NULL,
 content LONGTEXT NOT NULL DEFAULT '',
 UNIQUE (name)
);

Accessing content through our template

Example using the template and getPage PHP function: index.php <?php
  $dataDirectory = "/home1/andyd/site1Data";
  $templateFile = "/home1/andyd/site1Data/template1.htm";

 if(isset($name)){
   $pageText = getPage("$name");
   $pageTitle = str_replace(".html","",$name);
   $template = file_get_contents($templateFile);
   //These lines are odd but work (fill in the variables and ignore " marks)
    eval("print <<<END
 $template
END;
");
   eval("print(\"".$template."\");"); //eval includes variable values
 }else{
    print("<html><head><title>Index</title></head><body>");
    $d = openDir($dataDirectory);
    if($d){
     while( $file = readdir($d)){
      if( ereg(".html$", $file)){
       print <<<END
<a href="$PHP_SELF?name=$file">$file</a>
END;
      }
     }
     closedir($d);
     }else{
     // read file options from database
     }
     print("</body></html>");
 }
?>