π
This repository demonstrates how to format *.html.twig
(*.twig
) files that include Tailwind CSS classes using Prettier and relevant plugins.
The templates
folder contains 5 example files:
raw.html.twig
β Raw template without any Prettier formatting, but formatted using VS Codeβs built-in vscode.html-language-features
extensionanywhere.html.twig
β Formatted using @ttskch/prettier-plugin-tailwindcss-anywhere
anywhere.raw.html.twig
β Raw template, ready to be formatted using @ttskch/prettier-plugin-tailwindcss-anywhere
zackad.html.twig
β Formatted using @zackad/prettier-plugin-twig
zackad.raw.html.twig
β Raw template, ready to be formatted using @zackad/prettier-plugin-twig
[!TIP] You can format
anywhere.raw.html.twig
andzackad.raw.html.twig
usingpnpm format:fix
command or just by opening the file and saving it.
This project uses the pnpm
package manager.
[!NOTE] If you havenβt used
pnpm
before, check out the pnpm installation guide.
pnpm install
Check formatting:
pnpm format
Fix formatting:
pnpm format:fix
The configuration applies specific plugins based on filenames:
anywhere.html.twig
and anywhere.raw.html.twig
β Use parser: "anywhere"
with the @ttskch/prettier-plugin-tailwindcss-anywhere
pluginzackad.html.twig
and zackad.raw.html.twig
β Use parser: "twig"
with the @zackad/prettier-plugin-twig
plugin[!NOTE] These overrides are defined in
prettier.config.mjs
.
This project includes VS Code extension recommendations in .vscode/extensions.json
:
bradlc.vscode-tailwindcss
β Optional: Provides intelligent Tailwind CSS support in VS Code (class name autocomplete, linting, etc.)esbenp.prettier-vscode
β Required: Integrates Prettier into VS Code for consistent formattingThe project contains workspace-specific settings in .vscode/settings.json
that override your global VS Code preferences:
{
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[twig]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
// Adjust if using a Twig-specific VS Code extension
"files.associations": {
"*.html.twig": "html",
"*.twig": "html"
},
// Optional, see https://github.com/prettier/prettier-vscode?tab=readme-ov-file#prettierdocumentselectors
// "prettier.documentSelectors": ["*.html.twig", "*.twig"],
"prettier.enable": true
}