Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do almost all PHP framework use "<?php echo ... ?>"

  • The PHP short tag <?= $var ?> has been deprecated for a while.
  • Almost all PHP frameworks use the long form <?php echo $var ?> (e.g., symphony, Yii, Kohana)
  • Smarty is a famous PHP template engine which supports a shorter form {$var}
  • Template engines (like Smarty) are easier for web designer
    • Editing a template shows {$var} instead of showing nothing (because of <..>)
    • Shorter syntax (less typing especially when <> are on the same key on some keyboard layout)
    • The templates are pre-compiled and cached giving nearly the same performances

All those points make me wonder, why do all framework seem to use the super long PHP syntax? Is there a strong point of not using a template engine like Smarty (except the small overhead)?

like image 837
Wernight Avatar asked Feb 19 '10 06:02

Wernight


4 Answers

The thing about PHP is that it already is a templating language.

Smarty, as nice as it is, is added overhead. If you don't have a good reason to use it, then why would you? People who use backend frameworks are developers who are already familiar with PHP, so there's no reason to make them use a templating engine with a new syntax to learn on top of it.

Most frameworks are flexible enough that adding a template engine doesn't take much work. If a framework was built that forced you to use Smarty, then it's going to be less popular, because the framework itself would be less flexible.

In regards to the "long syntax", no framework is going to hang their hat on a deprecated syntax with security issues. It can be left up to the user of the framework if they want to use it or not (which no one should be these days), but building a core framework around short tags makes it less portable.

like image 139
zombat Avatar answered Nov 17 '22 10:11

zombat


I don't know that I would call <?php print $foo; ?> "super long syntax."

The fact is that short-tags aren't always enabled on servers, whereas the standard default typically is. It's safer going that route.

like image 45
Sampson Avatar answered Nov 17 '22 08:11

Sampson


Template engines like Smarty add an additional layer of processing that is not needed - they are mostly bloatware. They usually add too much additional processing for what amounts to syntactic sugar. Using a template engine when full PHP tags are available is like wearing shackles - it becomes another language with it's own quirks to learn in order to accomplish the same thing with regular PHP.

In my experience I've rarely seen a non-programmer fully or easily utilize a template engine. Take these two instances:

Smarty:

<select>
{foreach from=$k item=v}
 <option value="{$v.value|escape:'html'}">{$v.label|escape:'html'}</option>
{/foreach}
</select>

PHP:

<select>
<?php foreach ($k as $v) { ?>
 <option value="<?php echo htmlentities($v['value']); ?>"><?php echo htmlentities($v['label']); ?></option>
<?php } ?>
</select>

Now, the Smarty syntax may be slightly cleaner - but honestly, is anyone except a programmer going to be able work with either code set comfortably? Template engines add an extra layer of processing/logic without offering any major benefits.

like image 9
leepowers Avatar answered Nov 17 '22 09:11

leepowers


Here's what Fabien Potencier of the Symfony framework has to say about templating engines:

Why do people still think PHP is a templating engine? Sure enough, PHP started its life as a template language, but it did not evolve like one in the recent years. If you think PHP is still a template language, can you give me just one recent change in the PHP language which enhanced PHP as a template language? I cannot think of one.

He also describes the features that he looks for in a templating language:

  • Concision
  • Template oriented syntax
  • Reusability
  • Security
  • Sandbox mode

As well as some of the features that make his favorite templating language Twig stand out:

  • Native template inheritance (templates are compiled as classes);
  • Solid automatic auto-escaping (with no associated runtime overhead as everything is done during compilation);
  • Very secure sandbox mode (white-list the tags, filters, and methods that can be used in templates);
  • Great extensibility: you override everything, even the core features, by bundling your own tags and filters as an extension; but you can also manipulate the AST (Abstract Syntax Tree) before compilation. By leveraging this possibilities, you can even create your own DSL (Domain Specific Language), targeted at your application.

In the comments of the article, he says that, "It will probably be part of Symfony 2. But I first need some community feedback."

Read the full article to get his whole argument in favor of templating systems.

like image 5
Jakob Avatar answered Nov 17 '22 08:11

Jakob