-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Feature request: Twig support #5944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think it is out of scope of prettier (core). We support plugin system https://prettier.io/docs/en/plugins.html and real plugins with code how plugins should be implemented. I think the right decision would be to implement the plugin. |
Ah yeah, I totally forgot about plugins. Well, maybe it's the good time for me to write my first own plugin 👀 |
Thanks for issue, i closing the issue here because twig is not widely language for web, it is template engine for PHP, maybe we can this about this in https://github.com/prettier/plugin-php, feel free to open issue, i think it is good idea |
@allejo so as i describe about better put this into php plugin, feel free to open issue, i can help with this |
I don't feel that the PHP plugin would be the best place for this support since the syntax is used in so many other languages as well. If anything, maybe its own plugin? But I'd argue the closest relation of this would be a superset of HTML. |
I don't think it belongs in the PHP plugin either, definitely its own plugin and it's definitely more HTML with some bells than it is related to PHP. In fact the very design of the language is to prevent you from using any PHP in your views :p |
Reopen and mark as discussion |
Twig is also used as a templating language for a lot of Frontend projects which will have a Drupal Backend (which takes our components to render). This is also a pretty common case imo. but I think that the twig language syntax doesn't belong to the php, neither to the core/html part. The best would be to create an independent plugin for the language. |
I think it should be implemented as plugin, because it is not popular language for templates |
May I recommend this issue be renamed to something along the lines of "Jinja/Twig/Django/Liquid-like support" to avoid any further confusion? As mentioned, Twig by itself might not be hugely popular relative to the whole internet, but the syntax/markup itself is used by a lot of major CMSes/products around the world:
I agree that support for this would make sense as a plug-in. However, I strongly disagree that this syntax isn't popular. |
Twig can also be used in Wordpress with Timber plugin. |
We have good plugin api, any developer can start implement this |
It can also be used in any (standalone) Frontend-Stack with: |
We don't have enough developers support new language, better implement plugin |
I tried my hand at this some time back but while Twig itself doesn't have very complicated syntax I couldn't find how to make a plugin for a language that is a superset of another (here, HTML) personally. I'm not saying it's not possible I'm sure it is, but could be why we haven't seen a plugin pop up yet despite Twig's syntax being relatively basic |
Someone on Twitter linked me a link to Prettier plugin for Twig files: https://github.com/trivago/prettier-plugin-twig-melody |
I’ve been looking into this as well for Django Templates. As @allejo mentions there are a lot of templating languages that use nearly identical syntax, so it feels valuable to group the discussions about them, or even have a shared plugin implementation. Here are the template languages in question:
For all of the above here are the WIP plugins that have been built so far, all using different approaches:
I think it’s also worth mentioning other related plugins:
I didn’t spend much time assessing the plugins just yet but to me it feels like support for any or all of the above could be approached in a few different ways:
I’ve looked into differences between Django Templates and Jinja/Nunjucks before, and to me it feels like it’s 100% possible and desirable to have a single plugin that supports these three, and Nunjucks has the advantage of having an official JS parser. I didn’t look into Twig / Liquid much more to determine how close they are to Nunjucks, but it doesn’t feel like that big of a stretch to support them as well. If there was a single plugin supporting all of Django Templates, Twig, Jinja2, Nunjucks, Liquid, ideally backed by a battle-tested parser (rather than one developed just for the plugin), then it feels like there would be a better case for having this support built-in. I’m not sure how much time if any I can dedicate to this but to me it feels like a good next step should be to assess the existing plugins and see if (with the different approach each took) they would be likely to be compatible with other syntaxes. I presume individual plugin authors might find it easier to only focus on one of the template languages, but long-term it would be better to have a plugin support multiple syntaxes – if at all possible. |
+1 for Twig support, 👍especially when I am using twig and Atomic Design Systems (PatternLab) for the frontend! At the moment I am using friendsoftwig/twigcs via composer. |
Same!! |
I am just wondering what is the status of this thread, or how should we discuss this issue? 😊Or, are we waiting for a patch to test? |
@thibaudcolas Just an FYI, the approach I was taking using the Nunjucks parser was been tossed in the bin, and I've rewritten it to use mustache—which has significantly simplified the printer. It was a few months ago so my memory is hazy, but using the Nunjucks parser didn't provide enough information on the syntax itself. The most pertinent example I came across was there was no way to tell the difference between There were also issues with custom tags and being able to determine how they should be treated (there is still this issue to a degree, I've had to set up options to extend the "known" tags). The mustache approach could probably be genericised and adapted to the above-mentioned languages. (At my end progress has slowed because I just had another baby and they are time-sucks haha) |
@truls1502 @sghoweri Just clarifying: twigcs doesn't fix/format right? It just lints and reports style issues? |
Prettier isn't going to add more languages to the core. This should be implemented as a plugin. |
(Related to #4056 I suppose.)
On some large codebase, when many (old and new) developpers write/modify code, it's really borring and to review linting issues on Twig templates. 😞
I know there is also linting tools like
asm89/twig-lint
and the commandphp bin/console lint:twig
inside a Symfony project, but they only check if the syntax is valid. 😕It would be nice if Prettier can handle Twig files (
.twig
), but also HTML Twig files (.html.twig
), in order to have clean Twig files everywhere. 🎉The following elements that should be prettified:
{% %}
and{{ }}
tagsGiven the following snippet (taken from
admin/blog/edit.html.twig
from Symfony Demo, but uglified on purpose):The prettified should be something like this (this is my code style, but if Prettier on Twig files will be a thing, maybe we should ask to
twigphp/Twig
or the Twig community what they prefer):Thanks for your consideration, I'm sure that will be helpful to many people 🙂
The text was updated successfully, but these errors were encountered: