prettier-plugin-twig-enhancements

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript maintenance

A plugin for prettier-plugin-twig-melody that adds subtle formatting enhancements for Twig/Nunjucks templates. Current stable version is 1.0.1. It improves HTML element formatting to be more like Prettier (e.g., keeping short elements on one line, breaking long attributes) and prints empty {% block %} statements on a single line. No new releases since January 2023; the project is in maintenance mode as the author notes the Twig plugin is not well compatible with Nunjucks. Peer dependencies: prettier ^2.1.2 and prettier-plugin-twig-melody ^0.4.6. Key differentiator: addresses limitations of the base plugin for Nunjucks frontmatter and HTML formatting.

error Cannot find module 'prettier-plugin-twig-enhancements'
cause The plugin is not installed or not in node_modules.
fix
Run npm install --save-dev prettier-plugin-twig-enhancements.
error Invalid twigMelodyPlugins: Expected a list of strings
cause The plugin path is incorrect or configured under plugins instead of twigMelodyPlugins.
fix
Use "twigMelodyPlugins": ["node_modules/prettier-plugin-twig-enhancements"] in .prettierrc.
error Prettier version 3.x is not supported by prettier-plugin-twig-melody
cause Peer deps require prettier ^2.1.2.
fix
Downgrade prettier to 2.x: npm install --save-dev prettier@^2.1.2.
breaking v1.0.0 removed frontmatter support — if you relied on that for YAML frontmatter, you need to find an alternative.
fix Use a different tool like nunjucks-frontmatter-preprocessor before formatting.
deprecated The author states the Twig plugin is not well compatible with Nunjucks, making this enhancement less useful.
fix Consider using a Nunjucks-specific formatter like prettier-plugin-nunjucks or unformatted.
gotcha This plugin is not a Prettier plugin; it's a plugin for prettier-plugin-twig-melody. You must configure it under twigMelodyPlugins, not plugins.
fix Use twigMelodyPlugins array in .prettierrc.
gotcha Requires specific versions of peer deps: prettier ^2.1.2 and prettier-plugin-twig-melody ^0.4.6. Newer versions may not work.
fix Lock peer deps to those versions.
gotcha v1.0.1 changed behavior for empty HTML elements: now keeps <div></div> on one line instead of introducing a newline.
fix If you prefer the old behavior, pin to v1.0.0.
npm install prettier-plugin-twig-enhancements
yarn add prettier-plugin-twig-enhancements
pnpm add prettier-plugin-twig-enhancements

Install peer dependencies, configure .prettierrc, and format a Nunjucks template with the enhancements.

// Install peer deps and this plugin
npm install --save-dev prettier@^2.1.2 prettier-plugin-twig-melody@^0.4.6 prettier-plugin-twig-enhancements@^1.0.1

// .prettierrc
{
  "twigMelodyPlugins": ["node_modules/prettier-plugin-twig-enhancements"],
  "printWidth": 80,
  "tabWidth": 2
}

// Run on .njk file
npx prettier --write template.njk