Gathering detailed insights and metrics for @zackad/prettier-plugin-twig
Gathering detailed insights and metrics for @zackad/prettier-plugin-twig
Gathering detailed insights and metrics for @zackad/prettier-plugin-twig
Gathering detailed insights and metrics for @zackad/prettier-plugin-twig
Code formatting plugin for Prettier to handle Twig templates
npm install @zackad/prettier-plugin-twig
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
57 Stars
490 Commits
6 Forks
8 Watching
2 Branches
2 Contributors
Updated on 27 Nov 2024
JavaScript (84.8%)
Twig (14.71%)
Nix (0.38%)
Shell (0.11%)
Cumulative downloads
Total Downloads
Last day
9%
254
Compared to previous day
Last week
13.6%
1,384
Compared to previous week
Last month
153.7%
5,317
Compared to previous month
Last year
0%
9,275
Compared to previous year
Forked from trivago/prettier-plugin-twig-melody with focus on twig template only.
This Plugin enables Prettier to format .twig
files, as well as .html.twig
.
1yarn add --dev @zackad/prettier-plugin-twig
1prettier --write "**/*.twig"
In your editor, if the plugin is not automatically picked up and invoked (e.g., if you are using format on save, but no formatting is happening when you save), try adding the plugin explicitly in your Prettier configuration (e.g., .prettierrc.json
) using the plugins
key:
1{ 2 "printWidth": 80, 3 "tabWidth": 4, 4 "plugins": ["@zackad/prettier-plugin-twig"] 5}
This Prettier plugin comes with some options that you can add to your Prettier configuration (e.g., prettierrc.json
).
true
)Values can be true
or false
. If true
, single quotes will be used for string literals in Twig files.
true
)If set to true
, objects will always be wrapped/broken, even if they would fit on one line:
1<section 2 class="{{ { 3 base: css.prices 4 } | classes }}" 5></section>
If set to false
, this would be printed as:
1<section class="{{ { base: css.prices } | classes }}"></section>
true
)Follow the standards described in https://twig.symfony.com/doc/2.x/coding_standards.html exactly. If set to false
, some slight deviations might occur, such as spaces around the filter |
operator (s | upper
instead of s|upper
).
false
)Choose whether to output the block name in {% endblock %}
tags (e.g., {% endblock content %}
) or not. The default is not to output it.
[]
)An array of coherent sequences of non-standard Twig tags that should be treated as belonging together. Example (inspired by Craft CMS):
1twigMultiTags: [ 2 "nav,endnav", 3 "switch,case,default,endswitch", 4 "ifchildren,endifchildren", 5 "cache,endcache" 6]
Looking at the case of nav,endnav
, this means that the Twig tags {% nav %}
and {% endnav %}
will be treated as a pair, and everything in between will be indented:
1{% nav entry in entries %} 2 <li> 3 <a href="{{ entry.url }}">{{ entry.title }}</a> 4 </li> 5{% endnav %}
If we did not list the "nav,endnav"
entry in twigMultiTags
, this code example would be printed without indentation, because {% nav %}
and {% endnav %}
would be treated as unrelated, individual Twig tags:
1{% nav entry in entries %} 2<li> 3 <a href="{{ entry.url }}">{{ entry.title }}</a> 4</li> 5{% endnav %}
Note that the order matters: It has to be "nav,endnav"
, and it must not be "endnav,nav"
. In general, the first and the last tag name matter. In the case of "switch,case,default,endswitch"
, the order of case
and default
does not matter. However, switch
has to come first, and endswitch
has to come last.
prettier-ignore
and prettier-ignore-start
When you are not happy with how Prettier formats a certain element or section in the code, you can tell it to leave it in peace:
{# prettier-ignore #}
<div class="weird-formatting" >This will not be re-formatted</div>
<div class="weird-formatting" >But this will be</div>
You can also tell Prettier to leave entire regions as they are:
{# prettier-ignore-start #}
...
{# prettier-ignore-end #}
yarn test
to test against all regular testsNo vulnerabilities found.
No security vulnerabilities found.