Templates & PHP together in harmony

NOTE: There were a couple bugs in this code, but they’ve been fixed. Feel free to copy and paste this code (following the instructions), and test for yourself. The newly corrected code is in bold.

So a lot of people ask me how I seperate “business logic” from “display logic” in my sites and just in general. Usually you have two options, well, the third one (which i’m not going to show) pretty much defeats the purpose but don’t worry about that.

The first choice you have is to create functions referred to sometimes as “get” functions, such as the following:
<title><?php get_title(); ?></title>
or something like <?php get_pages_content($active_page); ?> you get the point. These functions are very specific and are obviously pre-built.

Second choice you have is to create a template file and make it with real content and then replace it with template variables encased in curly braces. Like {THIS} and {THIS_VARIABLE} actually, you can use whatever you want to encase the variables in dashes, brackets, percent symbols. Just for the record I use curly braces. Basically what you’re going to end up doing is replacing the variables with values and then echoing the entire file. It’s cool, so, check it out.

This whole routine is going to take 3 files.

  1. A template file (template.tpl)
  2. a class to handle the template file (the template class, this will be inside template.class.php)
  3. an index.php page to initialize the class and draw the data.

For the sake of length, I’m going to use the second method (The first one is too difficult and not as efficient)
Step 1 – Create the template file (usually .tpl just for consistency) we’ll call this file template.tpl

<title>{TITLE}</title>
<body>
{NAVIGATION}
{CONTENT}
</body>

Copy this and save it as template.tpl
Incase you didn’t notice, this is not well-formed XHTML. It’s just for space-sake.

The template class (save this as template.class.php)

class template {
public $template;

public function __construct() {
// load the templates contents into a variable
$this->template['TEMPLATE'] = file_get_contents('template.tpl');

// set some default values in the construct
template::set('{TITLE}', 'Page title here');
template::set('{NAVIGATION}', '<a href="index.php">Home</a>');
template::set('{CONTENT}'], 'Some example content');
template::set('{STYLE}'], 'sytle.css');
}
// a function for setting template variables values
public function set($variable, $value) {
$this->template[' . $variable . '] = $value;
}

public function render() {
echo (str_replace(array_keys($this->template), array_values($this->template), $this->template['TEMPLATE']));
}
}
?>

Save this as template.class.php

Now, the index.php file (make sure all of these files are in the same folder in order for htem to work correctly)

<?php
include(’template.class.php’);
$t = new template;
// render the template
$t->render();
?>

So save that as index.php and put it in your web root directory and open it up! You should see an ugly, but working page! Let me know if you have any problems. Now, if you want to add more template variables, go right ahead! If you want to change values on conditional statements, just use the set method like this:

<?php
include(’template.class.php’);
$t = new template;
$t->set(’{TITLE}’, ‘New page title’);
$t->render();
?>

Related reading

3 Responses to “Templates & PHP together in harmony”

  1. neverything  on August 30th, 2006

    Cool stuff and easy to understand. Possibly I’ll use it in a upcoming project. Thanks!

  2. bryan  on August 30th, 2006

    I know, it’s easily expandable too. We are in the process of getting an open source package hosting software on one of our servers so we can host all of this source code in a CVS/SVN directory so you guys can download compiled code instead of copying and pasting it together. Should be pretty soon.

  3. Seduction and Hypnosis  on July 29th, 2007

    Thanks for the article. A really interesting read. Keep in touch.


Leave a Reply