spudly.shuoink.com

the best way to predict the future is to implement it

Entries Comments


PHP Template Engine

9 February, 2008 (09:05) | PHP

I’ve been looking for a good PHP template engine for quite a while now, but I haven’t been able to find one that I feel good about. In my bout of frustration, I made my own. Mine, however, is based on normal php tags, rather than creating my own new template language. Thus, you can use real php functions within the template to format the data that is displayed. It’s also object oriented and supports caching.

Here’s a simple example:

PHP File:

<?php
   require('template.php');
   $template = new Template("./templates/");
   $template->setVars(array(
      "title" => "My Web Page",
      "hello" => "Hello World!"
   ));
   echo $template->process( "helloworld.html.php" );
?>

Template (HTML) File:

<html>
   <head>
      <title><?php echo $title; ?></title>
   </head>
   <body>
      <p>
         <?php echo $hello; ?>
      </p>
   </body>
</html>

Download Now!

Note: This is still somewhat a work in progress. On my todo list is 1) improve cache handling (based on request headers), and 2) redocument the source with phpdoc instead of perldoc.

« Pointers / References in JavaScript: a hack

 Steal These Buttons! - CSS Sprite Style »

Comments

Comment from Danonymous
Time: February 11, 2008, 1:49 am

Have you looked into Smarty Template Engine (http://smarty.php.net/)? If so, what kept you from using it?

Feel free to e-mail me.

Thanks.

Comment from spudly
Time: February 11, 2008, 3:20 pm

Actually, I have looked at it. I opted not to use Smarty for a number of reasons:

#1. I didn’t particularly feel like learning another language to create my templates. I am used to using Template Toolkit for perl, so I hoped to find a php implementation of it but realized that each programming language seems to have its own template engine(s), none of which used the same syntax as another. How dumb is that!

#2. PHP kindof is it’s own template engine. Simply use < ?=$var?> instead of [% var %] or smaty’s {$var}. Put your template in another file and include it at the end. So from that perspective, a new template engine like smarty isn’t really necessary for php.

#3. My solution is faster (I think), because it does not rely on parsing the template file and replacing the appropriate values. Instead, mine executes it as what it really is, a php file.

#4. My solution is more flexible (direct access to 700+ built-in php functions)

There is one drawback to using my approach; it’s not as secure. Smarty (i think) will allow you to restrict what can be done from within a template. If your templates are developed by end-users (not likely), they will have free range and may break something (intentional or otherwise). In my case, this is not an issue and I don’t ever foresee it becoming an issue.

Comment from Rasmus Schultz
Time: March 13, 2008, 9:44 am

I mostly agree with those four arguments, however, I went ahead and wrote my own template engine, which might interest you:

http://outline.mindplay.dk

#1. It borrows from PHP syntax, so you don’t have to learn another entire language. Most of the syntax is largely identical to PHP syntax, but without all the brackets and paranthesis, so it’s faster to type and easier to read.

#2. PHP is in deed a template engine - the {$var} syntax is just more legible and faster to type.

#3. Your solution IS faster, but not by much - my engine is around 200 lines of code, which is all that’s loaded, if your template has already previously been compiled. The total footprint of my engine is less 1000 lines of code. The compiled template output looks almost identical to your handwritten templates (which is what I used to use myself…)

#4. My template engine gives you direct access to all PHP functions, operators, classes, etc.

This engine is also not secure, but in my case, this is also not an issue.

I just wanted the convenience and legibility of a template engine whose syntax is optimized for layout logic - PHP’s syntax is more suitable for business logic.

That’s just my opinion, of course, but my engine has saved me lots of time already, my templates are more legible than my old pure PHP templates, and the performance hit is theoretical, at worst. It also includes a hierachical caching engine, which, if you apply it correctly, can make some pages even faster than straight-up PHP!

Write a comment