PHP Templating Engine

Apr 23, 03:46 PM—Micah Wylde

When building a website there are several important parts. One is the content; the reason for your site's existence. Another is the design; the look of your pages. A third, and the one I'm going to talk about today is the backend; the engine of your site. Using some simple PHP a world class Template Engine can be built in a few minutes.

A simple, powerful and easy to use PHP Template Engine? Oh yeah.

This template engine is so simple that it only requires two files. When creating accordionsoftware.com I spent quite a while researching CMS's and Template Engines, before deciding to just write my own. As far as I could tell all of the CMS's and other Template Engines were just useless fluff. The important code is only one line.

Why not use Smarty?

While many would be content to use a template engine already in existence, such as Smarty, this is not a good idea for several reasons. First of all is speed. If it is important to you to get every last drop out of your server, Smarty is not for you. These template engines are built on top of PHP, which means that first the Smarty engine has to be run by PHP, then your content has to be parsed by Smarty. This causes an extra delay between the time that your visitors call up your page and the time that it appears. Another reason why Smarty should not be used is that it uses it's own markup language for writing pages. For instance, to show an image you would write

{html_image file="image.gif"}

instead of the

<img src="image.gif" />

that we are all accustomed to. Why relearn in the Smarty markup what you already know how to do in HTML? By now you are probably wondering what the alternative is. This is it.

The Simple, Fast Alternative

The truth is, PHP is a template engine in its self. There is no difference between saying

{$name}

in Smarty and

<? $name ?>

for php. This is the premise that my Template Engine is built on.

On to the Code

By now you're probably wondering, 'How can such a simple template engine be written?' On to the code.

First of all, we need to write our template. Keep in mind that for the sake of understanding, these examples are kept very simple. This is what would go in the file 'template.php'.

<html>
<head>
<title> <?  echo($title) ?> </title>

</head>
<body>
<? echo($content) ?>
</body>
</html>

Note that you would change this to reflect your site. For instance here is the more complex template that powers accordionsoftware.com.

The next step is to create the individual page files. These will provclasse the data that personalizes the template. In keeping with the example template.php above, here it is:

<?php
//define our variables
$title = "The Title of this Page";
$content = "This is the content";

//now the magic
require('template.php');
?>

That's all. Of course you would need to change it according to your needs. For instance on this site I have a variable called $rightbar which is the info that goes on the right bar of the site. For a look at a more complex one, here is the index page at the front of my site. You would create one of those files for each page on your site. Simple, no? Here is the result of the examples above when run through the PHP Hypertext Preproccessor.

In Conclusion...

This technology is very versatile and can be used for any site. Using a template engine rather than simply writing out the html for each page saves countless hours whenever the time comes for an overhaul of you site. You can completely change the layout of your site by simply updating the template.php file.


Search:


This site is a collection of musings and articles on varied topics such as web standards/css, programming, digital rights, and, well, accordions.

Categories

Recent Articles:

Feeds

Links