10

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"?

temuri
  • 2,767
  • 5
  • 41
  • 63
  • No, because none of the responses are convincing enough. I've been working with Phalcon without Volt for ~2.5 years now and still don't understand why one would use it. – temuri Jan 27 '16 at 14:35
  • @temuri if you've been using something for almost 3 years without understanding why you're doing so, I'm sadly inclined to say that maybe IT/CS is not the best path for you... –  Apr 25 '18 at 16:30
  • https://www.schibsted.pl/blog/7-reasons-to-use-a-php-template-engine-for-your-webpage/ –  Apr 25 '18 at 16:32

4 Answers4

22

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.

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
  • For the performance hit, i hoped that finally we get a framework which compiles the template into C and loads them as so files. It's unbelievable that we waste the time on production systems where template never change except on relauches/staging events for development. 2015 and still room for tons of improvements. – Lothar Dec 12 '15 at 05:15
  • @Lothar You are correct, there is a performance hit there. Options for you: Run your app/compile your templates and then using those as source you can use Zephir (zephir-lang.org) to create .so files in your system and use them from there. A much easier approach would be to create a ramdrive or use the shared memory drive of a linux machine to be the storage of your compiled template files. Then you will only have to deal with the template files being read (before displayed) but that would be extremely fast because of the storage being in memory. – Nikolaos Dimopoulos Dec 29 '15 at 14:31
4

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.

noun
  • 3,635
  • 2
  • 25
  • 27
  • 5
    "to avoid mixing PHP with HTML" - IMO, there's not much difference between mixing it with PHP code or other sort of template mark-up. – temuri Jul 21 '14 at 14:11
  • 2
    There is a lot of difference, believe me. A template is much more easy to read, write, maintain, extend, etc. Try it without any prejudice and you'll see yourself. :-) – noun Jul 21 '14 at 22:31
  • To conform to the MVC pattern, this answer is correct. – Ash Apr 06 '16 at 11:19
  • 1
    @temuri maybe to you or to me there is no difference mix PHP code with template, but if you are not working alone... a designer or non technical person will thank you for use a template engine. – Lyoneel Jan 11 '17 at 11:57
  • @ash No it isn't correct! MVC pattern does *not* forbid you to use the underlying language for every layer of MVC directly! – rabudde Sep 18 '18 at 11:47
  • @rabudde it doesn’t, but, MVC is a pattern of concern separation to be simply put. Using php for this is a terrible implementation and therefore is _still_ bad practice because DOM shouldn’t be compiled as string values by any underlying language, elements within templates are objects in their own right. – Ash Sep 18 '18 at 21:31
  • 1
    @ash You're absolutely right. It's bad practise. I only want to point out, that using ` if (condition) { ?> } ?>` is technically the same as `{% if condition %}{% endif %}`. The main point should be, that *V* in MVC is an abstract layer which should only care about displaying user response. For that, native PHP can also be used to print objects that are in view scope, even there are better approaches like template engines which make templates look more well-formatted and are more intuitive to use for developers. – rabudde Sep 19 '18 at 06:54
  • @rabuddeI I agree with you, actually, I think I misunderstood originally (sorry ) – Ash Sep 19 '18 at 18:26
2

When history of PHP starts, it was standard, that HTML was generated directly inside PHP scripts. But it happen to generate some problems when it grown to one of most popular ever web programming languages.

To keep people working in huge projects comfortable with their tasks ie. when developing web services, where was an MVC architectural pattern invented. M for Model is used to store data and handle them in a maintainable and repeatable manner. V for View as an part, that is responsible to generate proper output. C for Controller that are responsible for sending commands to models etc., being a core logic of application.

When user send a command to service, Controller is handling it and understanding. Triggering changes on proper Models and maybe forwarding actions to other Controllers as long as View Controller is reached. View controller takes a proper view, injects into it generated data and executes it to create output that is send back to user. If template for view is not yet generates, it triggers template engine to do it.

Proper implementation of full MVC greatly helps both programmers, system architects, front-end devs etc. But when on one side it happen difficult to maintain databases (so ORM were developed), on other side of system it was difficult to maintain views.

One of principles of MVC is that Logic (with huge "L") is only in controllers. So there is no logic in views. Using template engine enforces easiness of achieving that because of poor support of logic by them self. It is not a secret, that programmers are "lazy" - so if they have an occasion to put some more advanced logic into view, they would do it.

I guess that all frontend devs have programming skills. Problem is to make them stay with you. Working on huge project with lots of views (and so hudge templates) with PHP mixed into them will drive them crazy. It is much easier to read code written for template engine.

Idea of template engines is to add one more part to system, that offers possibly simple language allowing to use basic logic like loops and if statements. On one side template engine receives kind of a textual file to transform it into PHP file with mixed into it hyper dose of all important HTML.

If you are alone developer, it is important to nobody that you are using files that mixes PHP with HTML. It looks probably close to this:

<html>
<head>
    <title><?=$title?></title>
    <? foreach($metas as $meta) { ?>
    <meta name="<?=$meta['name']?>" content="<?=$meta['content']?>"/>
    <? } ?>

But any frontend developer would rather like to work on file looking this way:

<html>
<head>
    <title>{{ title }}</title>
    {% for meta in metas %}
        <meta name="{{ meta.name }}" content="{{ meta.content }}" />
    {% endfor %}

for it higher readability. It happens extremely handy when template includes some kind of javascript inlined. After all, you end up with same file you can create by hand, but this file is generated for you and stored, so it is no more generated as long, as core file is not changed. We are generating our templates during deploy what has 0 hit on performance, especially now, when Volt engine is written in C.

Another handy thing about template engines is that (with some your effort) it is possible to generate templates minified on the fly. You wont ever minify your hand-crafted template, except when planning to commit suicide.

So globally, it is a case of:

so you could pass .volt files to a front-end guy

but it is an effect of working in MVC pattern. And Phalcon is made to fulfill MVC principles. I'm not even trying to guess, how you managed to work with this piece of software for ~2.5 years not using template engine. Maybe you are working alone.

Tyler
  • 742
  • 5
  • 11
yergo
  • 4,761
  • 2
  • 19
  • 41
0

Every person has different taste, but i tell you my vision.

I think volt is useful because:

I love the phalcon hierarchical rendering, because is clean, easy, and any person can understand where can find the view of something, ideal for a designer, or other non technical person

And to add value, i modify some phalcon parts to apply that hierarchical rendering using app modules, thats all i need.

Lyoneel
  • 419
  • 6
  • 16