Z
Z
zt502011-03-21 11:25:21
PHP
zt50, 2011-03-21 11:25:21

Twig vs Smarty

Hey!

I'm thinking of changing the template engine from Smarty to something else. Thinking about twig or even native.
There are not very many comparative characteristics of template engines (one seems to have slipped on Habré), twig seems to win there, but I did not find a single intelligible review about its real use.

Share your experience, please.

Answer the question

In order to leave comments, you need to log in

10 answer(s)
V
Vladimir Chernyshev, 2011-03-21
@VolCh

I tried twig and abandoned the “native templating engine” in new projects, although I have always been its ardent supporter. The main reason is that twig has output escaping enabled by default and {{user.name}} is much shorter (and clearer to the coder) than <?php echo htmlspecialchars($user->name, ENT_QUOTES, 'UTF-8') ?>. Plus, for convenient use of helpers and variables (at least replacing htmlspecialchars($user.name, ENT_QUOTES, 'UTF-8') with escape($user-name), and ideally just $user->name), requires an invasion of the global scope, which can lead to hard-to-find glitches.

A
Anatoly, 2011-03-21
@taliban

We have been working with smarts for a long time, with two and some other smarts, since there was a time when smarts did not develop, and we tried to find an alternative. Then we made a couple of projects without a template engine at all, and stopped there. If you approach this matter correctly, then using PHP as a template engine is not so expensive, and somewhere even convenient.

Z
zizop, 2011-03-21
@zizop

Why don't you want to switch to native? View - php-file itself.

K
Konstantin Kitmanov, 2011-03-21
@k12th

PHP as a template engine is quite convenient, because:
* a full-fledged language;
* obviously faster template engines built on its basis;
* does not require specific syntax support from your IDE/editor;
* almost everyone knows him. (If the coder does not know at least PHP at least at the “copy-paste and correct” level, then he is more trouble than good).
But there are also disadvantages:
* PHP as a templating engine is very verbose. Personally, as a front-end developer, I don't have much fun with all this, especially after Django templates.
* There is no block inheritance, because of this, calling partials with passing non-trivial parameters looks creepy, sometimes with a bunch of html in string literals.

Z
zt50, 2011-03-21
@zt50

Oh, yes, I completely forgot - it bothers me very much that the templates on Twig in the first compilation of the template (when there is no cache yet) are compiled for a very long time, longer than 30 seconds, but later, when everything is compiled very quickly and quickly. Thank you.

R
RomAndry, 2011-03-21
@RomAndry

Template engines were held in high esteem at one time, now any layout designer knows the basics of WEB programming and it is not difficult for him to use blocks of the same PHP code in templates.
But all the same, template engines rule and the habit remains. Have a look at the German vLIB
project for yourself (the direct link is not working yet)

B
bsboris, 2011-03-21
@bsboris

+1 for php as a templating engine. The main disadvantage of Smarty (and in general any third-party template engine) is that it will add an extra entity to the project. Web developers already need to know a bunch of languages ​​​​(offhand - html, css, js + framework, sql, server language + framework). At the same time, it is also unnecessary to remember the syntax of the template engine. In addition, php itself is really very convenient for use in templates, the main thing is to use the syntax like if (...): endif; instead of if(...) {} (may seem obvious, but, for example, one programmer friend likes to fence curly braces in templates. Looks terrible.)

K
Konstantin Kitmanov, 2011-03-21
@k12th

No "sandbox" is provided by the standard means of the language

Again, I am not a PHP expert, but if I remember correctly, it is quite possible to create a sandbox.

E
Elkaz, 2011-03-21
@Elkaz

fabien.potencier.org/article/34/templating-engines-in-php - comparison of template engines. I recommend Twig, due to the fact that it is much more convenient than any other PHP template engine, and faster (and more convenient) than native PHP.

C
criminalist, 2018-07-03
@criminalist

There is a post on Habré, Twig is an order of magnitude slower than Smarty, keep this in mind

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question