Kyle Florence Web Designer

tutorials

Intermediate Templating

  • Views2297
  • CategoriesPHP
  • Date PublishedNovember 9, 2003
  1. Basic Templating
  2. Intermediate Templating
  3. Advanced Templating and .htaccess

This is the second tutorial on templating, I am going to assume that you have already read the first and carry on with the rest of this tutorial. Now that you have learned the basics, it's time to show you how to build a site oriented around your templating system. In the previous tutorial I focused mainly on the templating class itself, and gave a small example that left many of you still confused. I hope to clarify some of that confusion with this tutorial.

First, open up your template, we need to add a small database class for use with MySQL databases. Here is the code, which I will explain afterwards.

class Database {
    var $query;
    var $link;
    var $result;
    var $row;

    function _clear() {
        $this->query = null;
        $this->link = null;
        $this->result = null;
        $this->row = null;
    }

    function _connect($servername, $username, $password, $name) {
        if (!$this->link = mysql_connect($servername, $username, $password)) {
            trigger_error("Database::_connect(); -> Could not connect to database: ", E_USER_ERROR);
        }
        if (!mysql_select_db($name)) {
            trigger_error("Database::_connect(); -> Could not select database: ", E_USER_ERROR);
        }
    }

    function _sql($sql) {
        if (!$this->result = mysql_query($sql)) {
            trigger_error("Database::_sql(); -> Query error: ", E_USER_ERROR);
        }
        return $this->result;
    }

    function fetch_row($result) {
        $this->row = @mysql_fetch_array($result);
        return $this->row;
    }
}

This is a VERY basic database wrapper class for use with MySQL. Basically, we have a few functions for each of the major operations you will be performing. We specify a _connect() function to connect to our database with a specified password, username, host, and database name. If there is an error, trigger_error() will display it for us, so you dont have to worry about putting the "or die" part on the end of your code. The next function is basically mysql_query(), or what you use to query your database. This also has error handling within the function itself. The last function is for fetching rows from your database based on whatever result is passed in.

We can set up our database and template classes like this:

$db = new Database();
$db->_connect('localhost','username','password','database');

$tpl = new Template();
$tpl->set_dir('./template/'); // this sets the template directory

Now that we have all that done (all this should be in the template.php file), save the file, and create a new one which we will call site.php. All of the .tpl (template) files i will have you create should be installed in a folder called "template." Make a new file called "header.tpl" and put the following code in it:

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>

Now create a file called "footer.tpl" and put the following code in it:

</body>
</html>

I'll keep it simple for now and keep the next files relatively small. Create 3 more files, "main.tpl", "about.tpl", "error.tpl", and "contact.tpl." You will fill each of these with the words "main", "about", "error", and "contact" accordingly.

Now, you are probably wondering how you can set up a whole website dynamically based on the templating system, the code below will show you how:

<?php
// Include the template class
include("template.php");

// prints the header
print($tpl->fetch($tpl->tdir.'header.tpl'));

switch ($_GET['p']){
    case 'main':
        // prints main.tpl
        print($tpl->fetch($tpl->tdir.'main.tpl'));
        break;
    case 'about':
        // prints about.tpl
        print($tpl->fetch($tpl->tdir.'about.tpl'));
        break;
    case 'contact':
        // prints contact.tpl
        print($tpl->fetch($tpl->tdir.'contact.tpl'));
        break;
    default:
        // prints error.tpl
        print($tpl->fetch($tpl->tdir.'error.tpl'));
        break;
}

// prints the footer
print($tpl->fetch($tpl->tdir.'footer.tpl'));
?>

You should already be familiar with everything in the above code. If you aren't, see the Basic Templating Tutorial and PHP's switch function(external link) for help.

This is the best way, in my opinion, to set up a website running a template class. It should be noted that you should not call variables from the URL that may result in unknown behavior. For example, using a variable in an SQL statement for the table name, as a user may enter another table name and be able to see information you may not want them to see. So be careful how you use this system.

By using this type of system, pages are accessed using the '?p=page' format, ie: http://kflorence.com/engine.php?p=links - The ? in an URL signifies a query string. In php, everything in a query string is set to a variable (depending if you have registered globals on or off, the variable p would be $p or $_GET['p']). In this particular query string, the variable $p would contain the string 'page', as it is set to page. To have multiple things in your query string, you use the & operator, ie: '?p=page&id=5'.

You should be getting a pretty good grasp on how the templating works, be sure to read the next tutorial, Advanced Templating and .htaccess, for information on how to make your website more search engine friendly by using Apache's mod_rewrite to tidy up your URL's.

question? comment? contact me