Writing a simple OO template system in PHP to separate logic and presentation

If you're maintaining a legacy PHP application which suffers from mixing of application logic and presentation, you may want to implement a simple template system to help separate these two.

Of course there are plenty of existing PHP templating systems, with Smarty being one of the most 'notorious' ;-). But if you have simple needs and don't want to introduce external dependencies to your code, rolling your own simple system can make sense. Let's face it -- you can often roll your own in the time it takes to figure out someone else's system.

Here are several posts which I found useful in this regard :

A good, clean, full overview of a simple OO (object-oriented) template system, written in 2008 :

- How to make a simple HTML template engine in PHP

A full treatment of a simple OO template system, compatible with PHP 4, written in 2005. Part II includes a simple caching system :

- Building a Template Parser Class with PHP, Part I

- Building a Template Parser Class with PHP, Part II

A good overview of a simple OO template system compatible with PHP 4, written in 2004. The above article written in 2005 is quite similar but perhaps explained better. Use the two-part article above before referring to this :

- Writing a Template System in PHP

Categories: 

There are 3 Comments

Learn to create a template engine is fun and a good training for better understand how they works, study a good one I think is a better pratice.
Here a nice benchmark that show the execution time with charts, you can also download it and add your template in the "race":

http://www.phpcomparison.net/