Implementing a template based website

Templates in a DB

Our example (which is the same method used on this site), uses a number of templates stored either in a MySQL database, or in a directory folder. 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:

<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 create a function to read these templates. In fact, we'll create two, one that uses the database method, and one that uses the directory method. You can use either, and both will work in the rest of the tutorial. First, lets create the database function.

function gettemplate($templatename) {
    global $templatecache;
    #check if template has already been loaded
    if ($templatecache[$templatename]!="") {
        #return cached version
        $template = $templatecache[$templatename];
    } else {
        #retrieve from database
        $query = mysql_query("SELECT content FROM templates WHERE name='$templatename'");
        if (mysql_num_rows($queryid)!=0) {
            #get the db field
            $template = mysql_result($query,"content");
            #replace \" with \\" ... this will be needed later in the script
            $template = str_replace("\"","\\\"",$template);
            #cache the contents
            $templatecache[$name] = $template;
        } else {
            $template="";
        }
    }
    return $template;
}

The function is very simple. First, it checks to see if we have a copy of the template in the cache (to save time accessing the db or file system again). If it hasn't, then it queries the database, and gets the content field. 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 templates, created using the following MySQL statement:

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

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell