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 :

Comments

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

http://www.phpcomparison.net/

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

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options