A Templating Class for PHP
Kick the HTML out of your PHP code!
Example and Source Code
A small example application using the template class can be found on the left side of this page. Other links let you view the source code of the example, the templates, and the templating class itself.Features
Here are some of the features of my templating system:- Object-oriented
For ease of use and data encapsulation, the code is written as a PHP class.
- Simple Variable Replacement
Displaying a PHP variable is as simple as inserting the following code in your template: <?=$variablename?>
This syntax is so simple that a designer with no programming experience can insert PHP variables into the template. - Variable Scoping
To prevent templates from accidentally accessing data, only the variables you have declared will be visible as local variables. If necessary, the template can access other variables through the PHP special global arrays.
- Template Caching
To increase performance for templates that are used multiple times, such as in a loop when building a complex table, the template files are read from disk only once, and thereafter read from a cache.
- Flexibility
In general, you should keep your PHP code separate from your HTML templates, but sometimes a little PHP can go a long way toward simplifying a complex task. You can use any valid PHP code in the template files.
Genesis and Revelations
PHP makes it easy to add programming code directly into your HTML pages, but for web applications this is a bad technique. It's better to keep your HTML in template files, separate from your application code. The template files can contain placeholders for the data and variables to be displayed.
While searching for a templating system for PHP, I encountered numerous suggestions:
- A fast template function.
The first solution I tried was a set of simple functions for loading template files. This did the trick, but was inelegant and limiting. Sometimes I wanted to do more than just place values into the template. (Yes -- the idea of templates is to keep code and display separate, but it can be very useful to bend the rules at times.)
- A complete templating system.
In search for more flexibility, I discovered several complete templating systems. These systems let you add all sorts of directives to your template files, almost like a new programming language. I thought this was rather silly, since it was just adding another layer of programming on top of PHP - if someone could learn a complex templating language, they could certainly learn a little PHP!
- Simple: just include the file with PHP!
So, full circle, I came to the conclusion that all of these templating solutions were worthless, because, with some self-discipline, PHP itself was a great templating system. But one problem remained: PHP has a nasty tendency when you include a file to immediately display the content of the file. This can be a problem when constructing a web page from multiple template files.
Luckily, PHP also has the ability to shut it's mouth when you tell it to: with output buffering you can temporarily redirect PHP's output streams and capture the data for future use. By using output buffering, and throwing in a few other features, I created a templating system with the features I desired.