Roll Your Own PHP Template Engine

PHP Template Engine

Follow along as we create a simple template engine without having to learn a whole new system.

Basics Of Page Generation

It puzzles me why PHP template processing has gone to the extreme. I mean, why are there so many template engines out there like Smarty, PHPTemplate, FastTemplate, PHPTal? They all are so complex and really is that necessary? PHP itself is a fine template engine. Why delve into someone else’s world when the PHP world is just fine.

First were static pages:

  1. <html>
  2. <body>
  3.   Hello, world!
  4. </body>
  5. </html>

Then, there were dynamic pages because someone wanted to grab data from a database or invoke a module. So people created scripting engines like Perl, and PHP:

  1. <html>
  2. <body>
  3.   <?php
  4.   $msg = "Hello, world!";
  5.   echo $msg;
  6.   ?>
  7. </body>
  8. </html>

Then All Hell Broke Loose

As projects got more complex and more people were added to the project, people got frustrated. The web design guys were angry at the developers because they were stepping on their layouts. The developers were angry at the web developers for overwriting their good code.

It was war. People complained and it got ugly. Everyone was touching everyone’s files and in most part, it was a disorganized mess. So a developer type worked on a compromise by separating his code with the HTML dude:

  1. <?php require_once("msgmachine.php"); ?>
  3. <html>
  4. <body>
  5. <?php echo $msgMachine->Generate(); ?>
  6. </body>
  7. </html>

But there was more complaints. HTML dude and developer dude were still stepping on the file because code and presentation were still mixed together.

Template Engines Everywhere

Then some developer types took things a step further – maybe too far. They created their own PHP template engines and jumped on their soap boxes to scream to the world “Look at me! Do it my way because its the best way!”.

So people listened and got sucked into the hype. Code looked different and ugly. It was like entering into someone else’s view of the world and when it came down to it, it still looked like PHP with funny additional tags.

Its All Textual Substitution

When you get down to the basics of dynamic page generation it boils down to taking a page and doing textual substitution, right? So why invest so much time learning another template language?

It just doesn’t make much sense. I mean, PHP itself does a fine job at that.

Its So Trivial, Really

Well I’m about to eliminate all the noise and show you how to roll your own PHP template engine. “Oh no!” you scream. “Not another solution to solve web developers and web designers complaints!”

Well, give me a minute. This solution is so trivial it’ll make you laugh and ask yourself how can one be so silly to fall victim to the PHP template engine hype.

My Solution. A Simple Solution.

Lets pick up where we were last. Look at this once again:

  1. <?php require_once("msgmachine.php"); ?>
  3. <html>
  4. <body>
  5. <?php echo $msgMachine->Generate(); ?>
  6. </body>
  7. </html>

Notice that the PHP file is including the module within the presentation file. Both web developers and designers will still step on each others toes in this file.

What if we created a very simple template engine class in PHP:

  1. template_engine.php
  2. ----------------------------
  4. class TemplateEngine {
  5.   var $templateFile;
  6.   var $variables;
  7.   var $result;
  9.   function TemplateEngine($template, $variables) {
  10.     $this->templateFile = $template;
  11.     $this->variables = $variables;
  12.     $this->result = "";
  13.   }
  15.   function Process() {
  16.     ob_start();
  17.     if (!is_null($this->variables))
  18.       extract($this->variables, EXTR_OVERWRITE);
  19.       if (!is_null($this->templateFile))
  20.         require_once($this->templateFile);
  21.       $this->result = ob_get_clean();
  22.   }
  24.   function GetResult() {
  25.     return($this->result);
  26.   }
  28.   function Show() {
  29.     echo $this->result;
  30.   }
  31. }

And then told the web designer types to go create template files with agreed upon variable stubs in them:

  1. index.tpl
  2. ----------------------
  4. <html>
  5. <head>
  6.   <title>My Home Page</title>
  7. </head>
  8. <body>
  9.   <h2>Message Of The Day</h2>
  10.   <?php echo $msg; ?>
  11. </body>
  12. </html>

Message Of The Day

Then let the web developers go out and build the PHP pages that serve as the glue to tie the presentation (index.tpl) with the dynamic page (index.php) and modules (msgofday.php):

  1. index.php
  2. ----------------------
  4. require_once(template_engine.php);
  5. require_once(module_msgofday.php);
  7. $msgOfDay = new MsgOfTheDay();
  8. $vars['msg'] = $msgOfDay->Get();
  9. $tp = new TemplateEngine("index.tpl", $vars);
  10. $tp->Process();
  11. $tp->Show();

The developers would only touch the glue file (index.php) and the module files (msgofday.php) while the designers would concentrate on the template files (index.tpl).

Isn’t that so easy? Thought so.

Of Interest