8 Mbits on the left lane
There are probably over a 100 different PHP template engines out there (the most famous one being Smarty). Templates are a good concept: keep the presentation and layout in one file, keep the business logic in another (and data storage in a third: the database). Use a simple and straightforward language to make templates, so that any graphics designer or HTML artist can use it, and leave the messy programming to something and someone else. Yes, templates are good with Java, C#, Python or whatever language you can think of. But not PHP.
Why is that ? Well here's a hint: PHP stands for "Hypertext Preprocessor". That sounds an awful lot like the description of a HTML template engine. And in fact, PHP is a sort of template engine: it was designed as a simple and straightforward language that even a graphic designer could use, and that you put directly inside your HTML to control the output (just as any other template language does). So what all those PHP template engine do is run a template engine on top of another template engine. Duh!
Consider for example this lines of a Smarty template:
{$title}
{include file='header.tpl'}
{if $a}
Hello
{else}
Bye
{/if}
And now the same thing in PHP:
<?php
echo($title);
include('header.php');
if ($a)
echo('Hello');
else
echo('Bye');
?>
I mean, what's the point of reinventing the wheel ?
Template engines requires the learning of a new syntax, slow down stuff (because you need to parse and process the template) and basically, as any extra layer of complexity does, increase the chances of hitting a bug or a security hole while making debugging harder. They only make sense in languages where outputting HTML is hard and painful. Is that the case with PHP ? Obviously not.
Of course, I'm not saying you should not separate presentation and logic. But you can do that without having to drop PHP: write a main PHP page with the presentation and include the logic in external files, or do the opposite and handle the output with templates written in plain PHP and HTML. Or do both and keep everything in neatly organized files.
Yes, you right.
But there are times template system can be quite cool.
For example, if you want users to change the template you don't want some PHP function to be called.
of course you can make a filter and take out all those PHP functions, but, then it would do the exact same thing as Smarty, but worse because users don't know which functions are ok.
So, for me, template system is all about security, else PHP can totally do the job.
Good point, security is indeed a valid reason to use a specific template language when dealing with user-submitted designs. But I believe it's a somewhat special case as most people seem to use templates for in-house designs.