PHP template engine

I’m looking around for php template engines (IE, Smarty, XTemplate, etc.) to use with our team site.

Which one(s) would you recommend and why?

I tried using Smarty, and I couldn’t get it to work. Granted, I already had an entire page created, and it was a hack and slash kind of conversion, and I didn’t put nearly as much effort into it as I should’ve. If you take the time to learn it though, Smarty seems to be pretty simple to use.

I’ve heard good things about patTemplate (http://www.php-tools.de/site.php?file=patTemplate/overview.xml) and SmartTemplate (http://www.smartphp.net/content/smarttemplate/about/about.html)… my experience with them is limited, but I liked how “lightweight” they felt.

There is always the “easy” option of just plain PHP. TBH, 99% of the time, auto_prepend, auto_append, and a normal PHP page will do the job just as well, all without having to learn a new language. When you pick up a template package, like it or not, you are usually learning a new programming language, and I have always felt that this is simply overkill. PHP is a fine templating language, it comes with blocks, if/else structures, and everything else you could possibly need. And don’t tell me that <!-- IF var -->…<!-- ENDIF --> is somehow simpler than <?php if ($var) { ?> <?php } ?>, they’re essentially the same thing in the end. Skip the process of converting templates into PHP programatically and just write PHP from the start. :-/

I remember looking into this a while back. I believe I came to the conclusion that Smarty had the best concept of what a template engine should be. That is, not simply an engine for replacing placeholders with relevant content, but an engine that facilitates the separation of application logic (getting the last 10 posters in this forum from the database) from presentation logic (increasing the size of a posters name if they are a moderator). However, Smarty is a feature rich template engine. So much so that it is becoming a programming language all of its own. This reality causes many programmers to cry down the use of Smarty, because they think it has lost sight of what a template engine should be. I have to admit, I took this position for a while, but as I thought about it more I realized that if a designer chose to limit himself (herself?) to the core elements of Smarty, then the overdone nature of Smarty becomes irrelevant. Added to that, the designer still benefits from some of the nicer features of Smarty (caching, etc…) that are not present in other template engines.

So I guess I’d recommend a controlled use of Smarty: use the elements you need, and ignore those that are unnecessary, or that will cause a deviation from the true purpose of a template engine.

PS: I ended up just writing my own template engine. Smarty was an excellent engine, but it lacked a few features that I really wanted to have (storing and retrieving templates from a database, etc…). Smarty was designed in a way that allows its users to add custom functions, so I could’ve integrated the missing features that way, but you know… lazy.

edit:

Something along the lines of:

<html>
<head>
  <title><var name="title"></title>
</head>
<body>
  <h1><var name="forumname"></h1>	
  Last 10 posts:
  <ul>
    <repeater name="posts">
      <li><var name="posts.title"> (in <var name="posts.parentthread">) <br /> <var name="posts.author"></li>
    </repeater>
  </ul>
</body>
</html>

is easier for a website designer to look at and grasp than:

<html>
<head>
  <title><?php echo $title ?></title>
</head>
<body>
  <h1><?php echo $forumname ?></h1>	
  Last 10 posts:
  <ul>
    <?php foreach ($posts as $post): ?>
      <li><?php echo $post'title'] ?> (in <?php echo $post'parentthread'] ?>) <br /> <?php echo $post'author'] ?></li>
    <?php endforeach; ?>
  </ul>
</body>
</html>

Why? Because web designers often know nothing beyond HTML/CSS. In addition, HTML is more like a structure providing language than an actual “programming” language, so the ability to understand, edit, and create HTML pages does not imply the ability to pick up on programming languages (even PHP, as simple as it is). Therefore, the template engine, should it want to be friendly to its user, should make its format resemble HTML as much as possible. My goodness, do you remember when you first had to grasp the concept of accessing elements of an array using square braces? Better yet, having to explain, in an understandable way, what square braces after a variable name does? Presentation does make a difference.

Smarty’s tags resemble PHP rather than HTML, and it does make it somewhat harder to learn (or explain to someone). But for this second response, I’m not talking about Smarty, but template engines in general.

PPS: I know PHP, but I would personally rather deal with the HTML-like template tags when designing pages. It may just have to do with my mindset.

If you’re looking for a very basic templating system, they’re pretty simple to write. The one I wrote is about 30 lines of code. You basically load all your data you want to put into the template into an array, then load your template, and search and replace. I’ve never used one of the already-made ones but I hear a lot of talk about Smarty. Like Joel said, it’s great if you keep it simple.