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

Posted on February 21st, 2010 by David Luhman and tagged .

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 :

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

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 :


Comment by Anonymous on Feb 22nd, 2011 at 6:55 am

Benchmark your template engine

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":


Comment by Ryn (not verified) on Apr 23rd, 2011 at 3:16 pm

Lint Template Engine

I implemented my own 'thin layer over PHP' templating engine: http://hardcoder.nazuka.net/projects/lint/

Comment by Passer By (not verified) on Jun 28th, 2013 at 1:24 pm