Why should I use templating system in PHP?

Josef Sábl picture Josef Sábl · Jan 12, 2009 · Viewed 13.1k times · Source

Why should I use templating system in PHP?

The reasoning behind my question is: PHP itself is feature rich templating system, why should I install another template engine?

The only two pros I found so far are:

  1. A bit cleaner syntax (sometimes)
  2. Template engine is not usually powerful enough to implement business logic so it forces you to separate concerns. Templating with PHP can lure you to walk around the templating principles and start writing code soup again.

... and both are quite negligible when compared to cons.

Small example:

PHP

<h1><?=$title?></h1>
<ul>
  <? foreach ($items as $item) {?>
  <li><?=$item?></li>
  <? } ?>
</ul>

Smarty

<h1>{$title}</h1>
<ul>
  {foreach item=item from=$items}
  <li>{$item}</li>
  {/foreach}
</ul>

I really don't see any difference at all.

Answer

Kent Fredric picture Kent Fredric · Jan 12, 2009

Yes, as you said, if you don't force yourself to use a templating engine inside PHP ( the templating engine ) it becomes easy to slip and stop separating concerns.

However, the same people who have problems separating concerns end up generating HTML and feeding it to smarty, or executing PHP code in Smarty, so Smarty's hardly solving your concern separation problem.

See also: