📜 ⬆️ ⬇️

Why I refused to use Smarty

A brief history excursion

When I came to work in one American office (remotely, of course, and this was the year 2000), I was forced to use the standards adopted by this organization. And one of them was the use of its template engine, which looked like a simple html file, which may contain special sequences of characters (usually starting and ending with "##"), which before being output to the browser will be replaced with texts or the results of other templates. Also there was its own API for working with such templates. Very simple API. And since I was very young at the time, I adopted these standards and began to use them in my work.

Here is an example of working with this template:
$template = new Template(); $template->Load('NameTemplate.html'); $template->Replace('##TITLE##', 'Hello world!'); $template->Out(); 


Acquaintance

Years went by. And during the implementation of the next project, the requirement has arisen: “Smarty must be used as a template engine”. The party said: "It is necessary." Komsomol answered: "Yes!". So I met Smarty. I really liked him. I was just beside myself with delight. Any task that I had to realize could be accomplished with its help. Sometimes simple, sometimes very difficult, but possible. In general, I began to use Smarty.

Epiphany

A few more years passed. I do not remember why, but there was a task to find an easy-to-use framework for php. I found their list and began to test them for our purposes. Naturally, one of the requirements was: Smarty support (and this was already my requirement). While reading the documentation of one of the frameworks (either Kohana, or CodeIgniter), I met a phrase like: “You can use Smarty, here’s how to connect it and how to work with it in our framework, but we think that native php is simpler, clearer and faster ... ". And I thought. Began to compare implementations on native php and Smarty.
')
Simpler? Of course, we already know php.
More clearly? Of course, we already understand php.
Faster? Of course, because the code on Smarty will be translated into the code on php (and at least it can not be faster, but slowly without difficulty).
Safer? I think yes. Although you can argue here. Holes can be done anywhere.

See for yourself:
 {$foo}  <?=$foo?> 


 {assign var=foo value='baa'}  <?php $foo = 'baa'; ?> 


 {include file='header.tpl'} -    php      <?php include 'header.php'; ?>     (   ) 


 {assign var="foo" value="`$foo+$bar`"} // ,     . <?php $foo += $bar; ?> 


I will not give examples of conditions and cycles - they take up a lot of space and look about the same.

I also remember how Smarty did the implementation of a recursive tree traversal, one of the options is to create a template and call this template inside. On php it looks like this:
 <?php function draw_tree($tree){ foreach ($tree as $node) { echo '<option ...>'.$node['name'].'</option>'; draw_tree($node['childs']); } } ?> 


I have long tried to convince myself that Smarty is more convenient for designers. But they did not come to him (for various reasons). And in the end, as a programmer, I had to write scripts for a scripting language. In addition, some versions of Smarty ended up with a vulnerability, and I, every now and then, had to go back to old projects in order to update the libraries and do compatibility checks.

Ps. Smarty has not been used for 2-3 years already, and therefore it is difficult for me to assess the current state of affairs, but I think things are no better and no worse than they were before.

Source: https://habr.com/ru/post/200198/


All Articles