Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 56,858 times

Contents

Downloads

Related Categories

Implementing a template based website - Templates in a DB

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)
);

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • using include with a php marked-up htm template

    Posted by aaron123nz on 02 Jun 2005

    I have been trying to use php's inlude to include a template file which has been marked up with some php variables. However when using include() the php parsing drops out. This code keeps the parsin...

  • Code Errors

    Posted by jaam on 25 Feb 2004

    This sample is great BUT have a cuople of errors.
    The file test.php is missing "#include template.php";.
    The file template.php presents a variable $name that must be repalced with $templatename.
    If...

  • Posted by James Crowley on 22 Feb 2004

    The article was written before PHP had made this a requirement - I'll try and get the article updated to reflect these changes. :)

  • Posted by James Crowley on 22 Feb 2004

    The article demonstrates two methods - one that uses a MySQL database to store the templates, and one that just uses a folder on the website.

  • Don't understand

    Posted by johnlcox on 26 Nov 2003

    I'm having a really hard time understanding how this works and I am not able to test it since I don't have a mysql server setup on my computer. Does anyone have an example they could show me that doe...