Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the benefits of using a template engine

Tags:

phalcon

volt

I do not understand why a developer would use Phalcon's Volt template engine.

In the end, after the compilation, the same PHP files are produced, that I would have to write manually in the first place. To me, it looks only to be detrimental to the performance.

Is the answer "so you could pass .volt files to a front-end guy"?

like image 683
temuri Avatar asked Jul 22 '13 00:07

temuri


2 Answers

The answer lies in the development of your application. Why do you use a framework instead of pure PHP? Why bother with object oriented programming when procedural/straight PHP is faster?

There are many reasons of course and it is a long discussion. The summary is ease of use and maintainability.

The same goes with Volt. You can use the volt templates to do what it will take you a lot longer if creating plain phtml files (HTML with PHP tags in there). Examples I can give you are template inheritance, partials, calculations within the template (for/each loops) etc.

As far as performance is concerned, there is always a performance hit when using a template engine. Volt luckily is part of Phalcon so the performance hit is minimal since Phalcon does all the hard work in memory instead of using included files here and there to offer its functionality.

The decision is up to you. Volt, Smarty, Twig and others are there to help with development of your application. Your decision is what makes you use the template engine or not.

like image 107
Nikolaos Dimopoulos Avatar answered Sep 18 '22 09:09

Nikolaos Dimopoulos


This is an old question, but I want add some insights.

You asked why you should use Volt, the Phalcon template engine, but in your explanation you want know, more generically, why you should use a template engine. The short answer to your question is: you must use a template engine to avoid mixing PHP with HTML.

But I want answer to the main question also. Why Volt? Volt overload is minimum compared to every other template engines out there and it's not because is written in C, but because it generates a unique PHP file for your view.

Twig is probably the most complete template engine out there. Twig has many more features compared to Volt, it's more stable and older. Twig, anyway, doesn't generate a unique PHP file, but a bunch of PHP classes with methods that call each others. Doesn't matter if you are using the Twig C Extension, Twig is gonna be slow anyway.

Twig is really really slow when compared to Volt and even to the good old Smarty. So, if you are using Phalcon is probably because you want achieve the best performances, serving a lot of page requests; in this case Volt is your friend.

like image 23
noun Avatar answered Sep 19 '22 09:09

noun