TLDR;
Install prettier npm packages and plugins:
|
|
Create prettier configuration file for “twig formatting” & “tailwind utility classes ordering”:
|
|
|
|
Add the formatting command as npm scripts for convinient of access:
|
|
Format using npm run format-all
command we just added:
Exclude files if necessary, through .prettierignore
file:
|
|
Use Prettier to Format Twig File
Till this date (2025-09-09) Prettier still doesn’t work natively with twig files (with *.twig
or *.html.twig
suffix).
Even after installing prettier with node package manager and the VSCode extension, and configure Prettier as the default formatter for twig files, you will still see warning that says: Configure Default Formatter Extension 'Prettier - Code formatter' is configured as formatter but it cannot format 'HTML (Twig)'-files
.
Configuration did beforehand the blow example:
Example formattting *.html
file:
Example formatting *.twig
file that yeild warning/error:
In order to use Prettier to format twig file, we will need to introduce the “@zackad/prettier-plugin-twig” plugin, you’ll need to first install as the development dependency, and add it in the .prettierrc
plugin parameter:
|
|
|
|
Then reload the VSCode window either via reopening or the “Developer: Reload Window” command, and re-try formatting the example.twig
file using “Format Document” command
(*sometimes if the “Format Document” command does not work, you can try the “Format Document (Forced)” command
Use Prettier to Sort Tailwind Classnames
Tailwind utility classes can can overwhelming when there are tens of them in one HTML component. With theprettier-plugin-tailwindcss
plugin, prettier could help sorting thes eytilities classes into tailwind’s recommended orders such that: classname orders are consistent throughout the codebase, the orders are more predictable (hence faster scanning for developers to stop the missing/extra utilities), reduce merge conflict during pull request.
Below are a list of ordering rules applied by the plug-in (according to ChatGPT):
- Group by category: Tailwind utility classes are ordered by their functional category (e.g., layout, flex/grid, spacing, sizing, typography, color, effects, others).
- Default ordering: Within each group, classes are sorted in a stable, deterministic order, often alphabetical or by a predefined internal order.
- Responsiveness and state variants: Variants (e.g., md:, lg:, hover:) are grouped and ordered consistently, often with the base class, followed by responsive variants in increasing breakpoint order, followed by state variants.
To use it for *.html
document you simply have to install it via npm install -D prettier prettier-plugin-tailwindcss
, and add it to the .prettierrc
file’s plugin:
|
|
However, the same formatting does not work on *.twig
files . This is still true even with the @zackad/prettier-plugin-twig
plugin, the formattting of prettier will work, but the ordering of class name is not included:
In order to have auto sorting of utility classes in twig, we’ll need to use the “ttskch / prettier-plugin-tailwindcss-anywhere” plugin, simply install it with npm via:
|
|
and modify the .prettierrc
file accordinly to use it as the parser:
|
|
Reload the VSCode window and retry formatting:
But please be aware that by default zackad/prettier-plugin-twig
uses twig
as the parser, so by setting the parser to anywhere, you looses some powers for format. For instance the tab alignment is no longer working in the below example:
To resolve this, I found a work around that is to format twice using two different configuration file:
|
|
(configuration files: .prettierrc-twig, .prettierrc-tailwind, the order MUST be prettierrc-twig
first, then prettierrc-tailwind
second)
Potentially you could save this as a npm script:
|
|
If some default files that comes with Drupal happen to throw error such as: Unexpected closing "div" tag. Seems like your DOM is out of control.
, you can put those in the .prettierignore
file to surpress them:
|
|
Reference
CodeKnight: Using Prettier with Twig in VS Code
- Explains how to use prettier to format twig file
GitHub Issue: Prettier Plugin - How to make it work for Twig files? (#11731)
- Explains how to use prettier to sort tailwind classnames in twig file
Prettier Plugin - For Formatting Twig File
- zackad/prettier-plugin-twig
- This Plugin enables Prettier to format
.twig
files, as well as.html.twig
. - forked from “trivago/prettier-plugin-twig-melody” (which is super outdated and only worked for prettier 2.X versions)
- zackad/prettier-plugin-twig focus on twig template only, work for the prettier 3.X versions.
- This Plugin enables Prettier to format
- (DO NOT USE !!!!) trivago/prettier-plugin-twig-melody
- works only for prettier 2.X versions
- at year 2025, this repo have not been updated in the past 5~6 years
- zackad/prettier-plugin-twig
Prettier Plugin - For Sorting Tailwind Class in Twig File
tailwindlabs/prettier-plugin-tailwindcss
- A Prettier v3+ plugin for Tailwind CSS v3.0+ that automatically sorts classes based on our recommended class order.
- See more on this official blog post: Automatic Class Sorting with Prettier
ttskch/prettier-plugin-tailwindcss-anywhere
A Prettier plugin for sorting TailwindCSS classes in any HTML-like language, like Twig etc⚡
It is dependent on the official “tailwindlabs/prettier-plugin-tailwindcss” plugin, that is to say, the “anywhere” plugin have to come after the “tailwindcss” plugin, below is an exmaple of such
.prettierrc
file:1
"plugins": ["prettier-plugin-tailwindcss", "@ttskch/prettier-plugin-tailwindcss-anywhere"],