PHP templating with… well… PHP itself!

Have you avoided adopting a templating engine for your web applications because you are afraid of the learning curve? You’re not alone. I was in the same boat about a year ago.

But, as applications evolve and grow more complex over their lifecycle, separating presentation HTML and “presentation logic” from application logic becomes essential to being able to manage your code.

The hot template engine out there right now is Smarty. Although it is fairly simple to adopt and use, it does require the use of a Smarty-specific syntax for implementing logic (such as if statements, which are essential if you are going to show/hide content and/or controls based on user roles) in the template files themselves.

And although Smarty’s website touts the performance benefits, lets not kid ourselves. Smarty consumes server resources and even if its the fastest engine out there, its still going to slow down your application.

The alternative is to simply use PHP itself as your templating engine.

What’s that? You don’t understand what I mean? Do you remember back when you first starting coding PHP, and you jumbled up your PHP and HTML together into the same files? Remember embedding the good ole “” ? That’s the basic principle at work in templating with PHP.

Brian Lozier’s excellent article, Beyond the Template Engine, which is from way back in 2003 (I know, an eternity in web terms) discusses these issues in more detail. He also includes a detailed tutorial in the use of a PHP4 class he wrote for PHP-based templating.

I have used this class in my own applications and have found it to be excellent. The class is extremely simple and can be easily updated to PHP5 standards. He’s even included a caching method in the class, which can significantly boost application performance and reduce database calls (which is what generates most of your application’s server load).

Just for kicks, I added a method to Brian’s class to pull the templates from a database table (see below). If you’re building an application for mass distribution (like vBulletin or WordPress, etc.), this is how you would build a user-editable template system.

public function fetch_from_db($file) {
     extract($this->vars);  // extract vars to the local namespace
     $sql = " SELECT * FROM content WHERE Name = '$file' LIMIT 1 ";
     $result = mysql_query($sql) or die (mysql_error());
     if(mysql_num_rows($result) == 0) {
          $sql = " SELECT * FROM content WHERE Name = 'pagenotfound' LIMIT 1 "; 
          $result = mysql_query($sql) or die (mysql_error());
     $row = mysql_fetch_object($result);
     eval("*>" . $row->Content . "<*"); // replace * with ? in your code
     $contents = ob_get_contents();
     return $contents;
delicious | digg | reddit | facebook | technorati | stumbleupon | chatintamil

3 thoughts on “PHP templating with… well… PHP itself!

  1. Pingback: Samuel Folkes: 17 PHP Practices That Should Be Banished Forever |

  2. Pingback: » Blog Archive » The problem with Database Abstraction Layers…

  3. Pingback: » Blog Archive » Fabien Potencier: PHP does need a template engine

Leave a Reply

Your email address will not be published. Required fields are marked *