Kyle Florence Web Designer


Basic Templating

  • Views2704
  • CategoriesPHP
  • Date PublishedSeptember 26, 2003
  1. Basic Templating
  2. Intermediate Templating
  3. Advanced Templating and .htaccess

Open up your favorite text editor and start a new file called "template.php." This file will contain your template class. Below is the code that will be put into the template file.

// very small template class, fast and gets the job done.

class Template {  
    var $vars;  
    var $tdir;  
    // sets the template directory and template
    function set_dir($template) {  
        $this->tdir = $template;  
    // define template file (constructor)  
    function Template($file = null) {
        $this->file = $file;  

    // set template variables
    function set($variable, $value) {  
        $this->vars[$variable] = is_object($value) ? $value->fetch() : $value;  
    // fetches a file and saves the output buffer, then returns it  
    function fetch($file = null) {
        if(!$file) {  
            $file = $this->file;
        $contents = ob_get_contents();
        return $contents;  

Although this code is very simple and basic, it has several key features which I think are essential. The first one is, it's not a memory hog. Many of the templating classes you find have way too many features, like how they parse their variables (many do it in the {VARIABLE} style). By using natural PHP language in your template makes your template speed FAST. Yes, you have to type a few extra characters (even less if you have short tags(external link) enabled), but its worth it in the speed. An example of a variable embedded in an HTML page may look like this:

<?php echo $var;?>

Your HTML may also contain more advanced structures, such as if/else statements or conditional loops (see: PHP: Escaping from HTML(external link))

Anyways, enough about that, lets get down to how you use this templating system. Start another file called "page.tpl". Insert this into the file and save:

<title><?php echo $title;?></title>
<?php echo $text;?>

As you can see this is a very basic HTML (template) file with 2 variables: $title and $text. We will need to set values to these variables via the template classes set() function.

Now that we have the needed files, lets go ahead and create a template directory somewhere, lets call it /template, then put "page.tpl" in that directory. In order for the template engine to know where your template files are located, you will need to set the directory. Before you can use any of the class functions however, you must first create an instance of the class. Make a new file called 'index.php' and put the following into it:

// This includes are template class into this file so we can use it.

// This assigns the variable $tpl to be an object of the class Template.
$tpl = new Template();

// This sets the template directory, /template as we called it.

// This is what sets the variable 'title' to hold the value 'test'
$tpl->set('title', 'test'); 

// This is what sets the variable 'text' to hold the value 'text'
$tpl->set('text', 'text');

That wasn't so hard, was it? Now we have the class initiated, and we have set values to our variables, all we have left to do is output the page. In the same file we put the other information in, add this line to the bottom:


This does basically what it looks like, fetches the file 'page.tpl' from your template directory, and prints it. Thats it! Of course things get more complicated as you add more features to your template, but this should help get you started. If you are interested in learning more about templating, be sure to check out the next two tutorials.

question? comment? contact me