magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Decide for one templating system (phtml or Twig)

Open mzeis opened this issue 13 years ago • 34 comments

As suggested by @Flyingmana I moved the discussion from #107 into a new issue.

In the mentioned issue, @sshymko said:

Most probably all existing phtml templates are going to be kept, no Twig templates are going to be provided out of the box. However, ability to create your own Twig templates is already there.

To which I responded:

Please stick with one solution: either phtml or Twig. I believe it will do more harm than good if you split up the community into one half phtml, one half Twig. More choices isn't always good.

I'm afraid "good" developers would use Twig, "bad" developers would use phtml. You will get a wild mix of two different templating systems.

Some reactions from others in the same issue:

@SchumacherFM:

Twig: +1 for @mzeis otherwise you will see in the phtml again a lot of business logic. it is too alluring to use incorrect and inperformant PHP patterns in the templates ...

@michaelgrosser:

Agreed with the above. A mix of template patterns is going to lead to a lot of the sloppy code and sloppy extension problems that exist in the Magento ecosystem (mainly 3rd party plugins) today. If the decision is to embrace Twig, it should be embraced across the board and not selectively.

@benmarks:

+1 for standardizing on one template idiom. Swappable template types could possibly be a "nice-to-have", but I'm more than happy to have Twig be the One True Template Type®.

@alex-m:

+1 for Twig, it's the only template format with logic that can be parsed with both JS and PHP that I've come accross

@philwinkle:

:+1: for standardization. :-1: for Twig. There are enough things to teach with regard to Magento; another idiom, templating, or meta-language isn't one I care to have to learn or instruct.

mzeis avatar Sep 25 '13 17:09 mzeis

thank you :)

two cons from me:

  1. twig does not prevent module creators from writing bad Templates. It makes it only harder by forcing them, to write a service/filter/pipe first, which makes the logic in templates even harder to read by hiding it. Also jumping to Models,Classes and so on is way harder to implement.
  2. Twig has an own LayoutBlock princip, in the symfony world it is not uncommon, that single templates change the content of multiple Blocks. This would affect very strong the current magento Layout system if implemented correct. Else we could only ignore the possibility, forbid it, or be ok with 2 co-existing layout systems.

I personally prefer mustache, as it is language independent and is easy usable via javascript and a big pro for ajax applications. But still Iam for keeping phtml as main template language. It keeps the entry level low for new developers and its easier to read when good written.

any other template language is already easy to implement by adding a simple block class.

Flyingmana avatar Sep 25 '13 18:09 Flyingmana

I think my biggest reason for supporting a move to Twig or something similar is that I'm fairly tired of seeing code with Mage::getModel('some/thing')->getCollection() done straight in a phtml file. Twig would at least prevent that. While I agree that it doesn't safeguard against all sloppy coding, the barrier to entry of having to go hack up a service class is much higher. Typically, I'd say this is high enough to prevent a lazy developer from going rouge and doing things in the template that don't belong there for the sake of expedience.

Troubleshooting becomes much more difficult when you have to start hunting for model requests and collection builds in phtml files versus at least knowing the code is in a defined place in which it belongs, even if it is sloppy.

michaelgrosser avatar Sep 25 '13 18:09 michaelgrosser

+1 for twig (or anything to get away from .phtml files).

rgranadino avatar Sep 25 '13 21:09 rgranadino

I'm fairly tired of seeing code with Mage::getModel('some/thing')->getCollection() done straight in a phtml file.

I'm tired of seeing any imperative code in templates, it should be entirely declarative like XSLT because HTML is also declarative. Symphony (not Symfony) does this and is arguably more future-safe for it.

clockworkgeek avatar Sep 25 '13 21:09 clockworkgeek

+1 for twig to reduce possibility of xss attacks on the view layer.

airbone42 avatar Sep 26 '13 12:09 airbone42

+1 for standardisation and +1 for Twig

My only concern is that Twig being more complicated will encourage more "hack and fudge" development ;)

duffybelfield avatar Sep 26 '13 22:09 duffybelfield

+1 for having a single templating system -1 for Twig.

I don't care to learn or teach a new language. Pure PHP does the job better than any templating language out there.

Though I am also tired of seeing crappy code in templates, its a problem that has a simple fix which users of Git have come to know as "hooks". Simply reject any commits with Mage::getModel or similar inside of a of a phtml file. Of course, that would reject a lot of Magento built in templates (price.phtml I'm looking at you) but "good" programmers don't really need to keep those under version control, right?

The truth is that "having the ability" to write crappy quick hacky code into templates is actually an asset that has allowed me to code proof-of-concept prototypes that I can show my customer for him to decide whether to go forward with one funcionality or another in a very easy quick way.

In my company, we have NEVER used a community module's templates as provided. Having two template engines would mean that every time I download a new community module, it would be a flip of a coin whether it uses the same engine as the rest of my codebase or the other (thus making it useless or less than simple to implement). Then the dicussion will turn to "should all modules provide both alternatives?", which will basically be moving the problems I am seeing as a Magento solution agency to the Magento module agencies, but not really taking away the issue.

Now, if there are REALLY good reasons to move forward with Twig (or whatever templating engine) then that should be the only option available and the whole of Magento templates should be changed. The way I see it, going half the way (adding support but not endorsing it from the core) could have a negative impact in the Magento developer comunity as a whole

barbazul avatar Sep 27 '13 13:09 barbazul

Please only support one template system, allowing multiple will lead to unnecessary confusion and division. I'm not too concerned about which template system, my personal preference would be Twig, mostly for the reasons already mentioned.

joshdw1 avatar Sep 27 '13 14:09 joshdw1

+1 for Twig, and for only ONE option available.

Thanks

centerax avatar Oct 01 '13 21:10 centerax

:thumbsup: for standardization and :thumbsup: for twig.

The purpose of Magento 2 is to modernize the stack without as much burden for backwards compatibility. Yes there will be a little bit of a learning curve, but much of the PHP community has already moved to using twig.

One of the things I like a lot about it is the way that you can do inheritance. I think in some ways it has a better block / layout system than Magento's native one.

kalenjordan avatar Oct 03 '13 17:10 kalenjordan

I think many of developers are in this topic have more free time to learn something new. What is your pros and cons about phtml? Nobody explain of that. As you might guess, Magento's herself complex system and many developers are spent so much time learn. My choice, leave as it is.

artlantis avatar Oct 03 '13 17:10 artlantis

much of the PHP community has already moved to using twig

[citation needed]

clockworkgeek avatar Oct 03 '13 17:10 clockworkgeek

Having 2 different template Will only cause confusion on long term.

I also support moving forward with twig only

Sent from my iPhone

On 3 Eki 2013, at 20:56, "clockworkgeek" <[email protected]mailto:[email protected]> wrote:

much of the PHP community has already moved to using twig

[citation neededhttp://xkcd.com/285/]

Reply to this email directly or view it on GitHubhttps://github.com/magento/magento2/issues/370#issuecomment-25642956.

mertgokceimam avatar Oct 03 '13 18:10 mertgokceimam

@clockworkgeek Well played :) Primarily I had symfony in mind when saying that.

kalenjordan avatar Oct 03 '13 18:10 kalenjordan

@clockworkgeek +1 you made my day :-)

+1 for just one template engine standard. Keep it simple stupid. Prevent conflict, misunderstanding and integration problems.

If the reason to change the template engine is to prevent bad practices, bad news, do not use PHP! It's full of it.

Twig is mainly used by the Symfony community, it's a good engine and prevent adding PHP code into X/HTML templates. However it doesn't prevent to put all HTML code in a twig variable!!! You can still have bad practices.

Phtml template have already been used by Magento 1.x, and the doc to customize it is widely spread. As it was said, I totally agree to prevent adding complexity to the complexity. Magento has enough complex design patterns which load the charge of work to not add a new layer of difficulty.

Personally, I don't see why change. Even if I like Twig and use it for Symfony projects, it's a solution for new projects, not for an existing one and so established as Magento.

The fact is, it will add more cost to deliver a Magento project. As least, during the first year(s) of existence.

sylvainraye avatar Oct 03 '13 19:10 sylvainraye

+1 for having one standard template engine and I prefer to use twig since template files should be simple and easy syntax. But having template files in PHP, make bad developer doing more things in template level.

Also, if the worry is jumping around classes in Twig. Twig still be able to do it. It's just need to do it in proper way.

dimasdwika avatar Oct 03 '13 22:10 dimasdwika

-zillion for having a template engine in a template engine in a template engine. Did u read @barbazul before commenting? This thread is like "Oh, I know Twig, I vote for it."...

You can have proper, clean, flexible and maintainable PHP but you like to choose another overhead and another instance over performance and KISS? I can count the amount of people that like to change magento core / templates even without having hands (for those who still noodle over it: it's zero).

Go back to the roots and think about it very carefully. Just like knowing and loving Twig is not enough to make it a standard. Enumerate pros that PHP already have is neither a reason to decide. Did you already define some in front standards? Is it Push- or Pull- between the Model and the View? Just realized the waste of time for this discussion, because there are no upstream standards. Old problem: Talking about the flowers on the balcony while the foundation of the house is slobber.

Gimme one good reason why Twig. That some developer of extensions or script kiddies can't behave and messed up phtml is not an option. How difficult do you like to make Magento anyway?

{% endrage %}

ScreamingDev avatar Oct 04 '13 08:10 ScreamingDev

Summary: +1 for Standardisation -1 for Twig.

I love Twig, but I'm not entirely comfortable with the idea of it being a default.

My main reservation about Twig, is that it is a current fashion. So it's far from impossible that a newer, trendier, templating language could spring up in a few years.

Just think if Magento had used Smarty as its templating engine way back in 2007? That would feel /very/ gross now. I feel a phtml engine for all its drawbacks (and there are many), is at least flexible in the face of future changes to best-practice and within the PHP language itself.

Another parallel that comes to mind is that of Prototype vs JQuery. When Magento was being developed the js/dom abstraction layer wars were not resolved. And while it's arguable, even today, that Prototype is technically superior, JQuery won the war for developer and designer mindshare. So we see now JQuery jerry-rigged into Magento, but it's not a comfortable fit and it is far from a first class citizen. I fear something potentially similar happening with templating if we went down the Twig route.

Allowing developers to plugin their own templating engines would be nice. But I feel only a single engine should be officially distributed and supported out of the box.

ajbonner avatar Oct 04 '13 09:10 ajbonner

Invoking smarty...well played, @ajbonner . I think you sold me.

@sourcerer-mike, pretty sure you're only allowed to minus a maximum of one at a time :smile: - yes I read @barbazul 's comment. For me, preventing people from doing bad stuff isn't the primary motivation behind wanting Twig. Having the ability to writer cleaner templates is.

kalenjordan avatar Oct 04 '13 14:10 kalenjordan

Twig is not only used by Symfony, but by many other big Open-Source projects like Drupal, phpBB, eZPublish; projects from the Magento community like OroCRM and Akeneo; and many other smaller ones like Piwik, doclear, Craft, ...

Just my 2cents ;-)

fabpot avatar Oct 08 '13 10:10 fabpot