Smarty is one of the oldest PHP templating engines. If you are programming in PHP - most likely you had to work with it. In 2010, the third version of this template was released. Smarty 3 was written from scratch, with active use of PHP5. At the same time, Smarty received an updated syntax and modern features, including
inheritance ,
sandbox , etc.
Twig is a young template engine from symfony developers. The authors
position it as a fast and functional template engine. By its capabilities, it is in many ways similar to Smarty 3. Twig is distinguished by a slightly different syntax, as well as the declared high performance. Check?
Testing
When testing, we will intentionally use quite complex patterns so that the processing time will be noticeable. Actually, this time we will evaluate, for which we will prepare the appropriate scripts.
The code for Smarty is very simple:
$data = json_decode(file_get_contents('data.json'), true);
require('smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->compile_check = false;
$start = microtime(true);
$smarty->assign($data);
$smarty->fetch('demo.tpl');
echo microtime(true)-$start;
Twig :
$data = json_decode(file_get_contents('data.json'), true);
require('twig/Autoloader.php');
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => 'templates_c',
'autoescape' => false,
'auto_reload' => false,
));
$start = microtime(true);
$template = $twig->loadTemplate('demo.tpl');
$template->render($data);
echo microtime(true)-$start;
: , .
— . . , , . - . — . , 10000 .
Smarty:
{$var0} {$var1} {$var2} {$var3} {$var4} ...
Twig:
{{ var0 }} {{ var1 }} {{ var2 }} {{ var3 }} {{ var4 }} ...
:
| | |
---|
Smarty 3.1.1 | 16.320 . | 0.058 . |
Twig 1.2.0 | 9.757 . | 0.083 . |
. , . Smarty Twig. , , , . Smarty ≈30% .
- foreach. , 10 1000 .
Smarty:
{foreach $array as $item}
{$item.id} {$item.title} {$item.var1} {$item.var2} {$item.var3} {$item.var4} {$item.var5} {$item.var6} {$item.var5} {$item.var6}
{/foreach}
Twig:
{% for item in array %}
{{ item.id }} {{ item.title }} {{ item.var1 }} {{ item.var2 }} {{ item.var3 }} {{ item.var4 }} {{ item.var5 }} {{ item.var6 }} {{ item.var5 }} {{ item.var6 }}
{% endfor %}
:
| | |
---|
Smarty 3.1.1 | 0.065 . | 0.009 . |
Twig 1.2.0 | 0.131 . | 0.082 . |
: Smarty 10 , Twig! , + Smarty Twig. , Smarty Twig, , .
— . - :) , Smarty Twig. 500 , 500 , , 500 . .
:
| | |
---|
Smarty 3.1.1 | 1.329 . | 0.002 . |
Twig 1.2.0 | 2.641 . | 0.121 . |
Smarty 60 . , , . Smarty , . ! Twig , .
: Smarty Twig. , .
. Pentium Dual-Core T4200 (2 GHz), 3GB RAM — . PHP — 5.3. , Smarty Twig ,
.