包详细信息

@zackad/prettier-plugin-twig

zackad26.1kApache-2.00.16.0

Prettier Plugin for Twig

自述文件

Prettier Plugin for Twig

Forked from trivago/prettier-plugin-twig-melody with focus on twig template only.

Prettier Twig Banner Prettier Twig Banner

GitHub Actions Workflow Status GitHub License NPM Version


This Plugin enables Prettier to format .twig files, as well as .html.twig.

Installation

yarn add --dev @zackad/prettier-plugin-twig

Configuration

Add the plugin explicitly into your Prettier configuration (e.g., .prettierrc.json) using the plugins key:

{
    "plugins": ["@zackad/prettier-plugin-twig"]
}

Usage

./node_modules/.bin/prettier --write "**/*.twig"

Options

This Prettier plugin comes with some options that you can add to your Prettier configuration (e.g., prettierrc.json).

twigSingleQuote (default: true)

Values can be true or false. If true, single quotes will be used for string literals in Twig files.

twigAlwaysBreakObjects (default: true)

If set to true, objects will always be wrapped/broken, even if they would fit on one line:

<section
    class="{{ {
        base: css.prices
    } | classes }}"
></section>

If set to false, this would be printed as:

<section class="{{ { base: css.prices } | classes }}"></section>

twigFollowOfficialCodingStandards (default: 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).

twigOutputEndblockName (default: false)

Choose whether to output the block name in {% endblock %} tags (e.g., {% endblock content %}) or not. The default is not to output it.

twigMultiTags (default: [])

An array of coherent sequences of non-standard Twig tags that should be treated as belonging together. Example (inspired by Craft CMS):

{
    "twigMultiTags": [
        "nav,endnav",
        "switch,case,default,endswitch",
        "ifchildren,endifchildren",
        "cache,endcache"
    ]
}

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:

{% nav entry in entries %}
    <li>
        <a href="{{ entry.url }}">{{ entry.title }}</a>
    </li>
{% 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:

{% nav entry in entries %}
<li>
    <a href="{{ entry.url }}">{{ entry.title }}</a>
</li>
{% 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.

twigTestExpressions (default: [])

Make custom Twig tests known to the parser.

{
    "twigTestExpressions": [
        "snake_case_test",
        "camelCaseTest"
    ]
}

Example

{{ a is snake_case_test }}
{{ a is not snake_case_test }}
{{ a is camelCaseTest }}
{{ a is not camelCaseTest }}

Features

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 #}

Testing

  • You can call yarn testto test against all regular tests

Credit

更新日志

Changelog

unreleased


0.16.0 (2025-03-17)

Features


0.15.3 (2025-02-22)

Bugfixes

  • GH-127 Fix missing ignore missing part of include statement

0.15.2 (2025-02-14)

Bugfixes

  • GH-125 Add missing xor binary operator

0.15.1 (2025-02-12)

Bugfixes

  • GH-123 Fix parsing negative number

0.15.0 (2025-02-01)

Features

  • GH-61 Add support for twig comment as html element attribute
  • GH-116 Add possibility to register any custom Twig test expression

Bugfixes

  • Fix broken embed statement with only modifier

Internals

  • Stringify ast object to make it easier to inspect when debugging on test setup

0.14.1 (2025-01-04)

Bugfix

  • GH-110 Fix regression when parsing concatenation expression inside html attribute value

Internals

  • DX: Don't use alias to export print function to make it easier to identify when debugging call stack

0.14.0 (2024-12-18)

Bugfixes

  • GH-90 Properly implement bracketSameLine options for html element tag

Input

<iframe class=""
        src="https://www.google.com/maps/embed"
        frameborder="0"
        allowfullscreen></iframe>

<img src="/public/logo.png" alt="some description that should be enough to break this into multiline" class="block bg-white border radius-lg"/>
<img src="/public/logo.png" class="block bg-white border radius-lg"/>
<a href="/homepage" class="block bg-white border radius-lg" aria-label="some text label">some text</a>

<br />

bracketSameLine: true

<iframe
    class=""
    src="https://www.google.com/maps/embed"
    frameborder="0"
    allowfullscreen></iframe>

<img
    src="/public/logo.png"
    alt="some description that should be enough to break this into multiline"
    class="block bg-white border radius-lg" />
<img src="/public/logo.png" class="block bg-white border radius-lg" />
<a
    href="/homepage"
    class="block bg-white border radius-lg"
    aria-label="some text label">
    some text
</a>

<br />

bracketSameLine: false

<iframe
    class=""
    src="https://www.google.com/maps/embed"
    frameborder="0"
    allowfullscreen
></iframe>

<img
    src="/public/logo.png"
    alt="some description that should be enough to break this into multiline"
    class="block bg-white border radius-lg"
/>
<img src="/public/logo.png" class="block bg-white border radius-lg" />
<a
    href="/homepage"
    class="block bg-white border radius-lg"
    aria-label="some text label"
>
    some text
</a>

<br />
  • GH-101 Fix printing some html tags to be on their own line (heading and table cell)
  • GH-106 Fix handling string literal when using block shortcut syntax

Internals

  • Add option to add prefix/suffix for test snapshot output. This will allow to reuse single input file to produce several snapshot output with different configuration

0.13.0 (2024-12-09)

Features

Bugfixes

  • Fix indentation for html attribute when printed on multiline. All attribute will be indented on each line.

Input

<iframe class=""
    src="https://www.google.com/maps/embed"
    frameborder="0"
    allowfullscreen></iframe>

Output

- <iframe class=""
+ <iframe
+     class=""
      src="https://www.google.com/maps/embed"
      frameborder="0"
      allowfullscreen></iframe>

Internals

  • Remove unused dependencies resolve

0.12.0 (2024-11-22)

[!IMPORTANT] Yet another breaking changes. See release notes or changelog before upgrading.

BREAKING CHANGES

  • Option twigPrintWidth has been removed

I don't see any reason why we should keep the option twigPrintWidth. Prettier provide mechanism to use different printWidth for different file types by using override (see example below).

What to do?

Remove twigPrintWidth from your prettier config and use override instead.

Example

  # ./.prettierrc.yaml
- twigPrintWidth: 120
+ overrides:
+   - files: "*.twig"
+     options:
+       printWidth: 120

Features

  • Add support for spread operator

Example

<twig:Component {{ ...vars }} />

{% set numbers = [1, 2, ...moreNumbers] %}
{% set ratings = {'q1': 10, 'q2': 5, ...moreRatings} %}

{{ 'Hello %s %s!'|format(...['Fabien', 'Potencier']) }}

Bugfixes

  • Make sure that empty block statement will have empty line in between for consistency

0.11.1 (2024-11-13)

Bugfixes

  • Fix handling empty string as default value on macros declaration

0.11.0 (2024-11-12)

[!IMPORTANT] Twig coding standard has been updated to version 3.x. This contains BREAKING CHANGES.

BREAKING CHANGES

This release contains breaking changes, check your setup before upgrading. Option twigFollowOfficialCodingStandards is set to true by default. If you're using twig version 3.11 or older it will break your setup. Notable changes on twig coding standard on version 3.x is added support for using colon : as separator for argument names and values (which is enabled by default with this plugin).

Example

{# Input #}
{{ data|convert_encoding(from= 'iso-2022-jp', to: 'UTF-8') }}

{# Before #}
{{ data|convert_encoding(from = 'iso-2022-jp', to = 'UTF-8') }}

{# After #}
{{ data|convert_encoding(from: 'iso-2022-jp', to: 'UTF-8') }}

What to do?

  • If you want to upgrade this plugin, make sure that you're using twig version 3.12 or later
  • If you want to keep using older version of twig, you can set option twigFollowOfficialCodingStandards to false (your code style might become inconsistent)

Features

  • BREAKING CHANGES: Update twig coding standard to follow version 3.x

0.10.0 (2024-11-11)

Features

  • Add support for default value on macros
  • Add support for test expression instance of, feature of Craft CMS
  • Add support for colon as separators between argument names and values

Internals

  • Test with Node.js 22, current active lts version
  • Add smoke test to make sure that the plugin can be loaded properly

0.9.1 (2024-10-10)

Bugfixes

  • Fix importing lodash function syntax
  • Fix import template function from @babel/template

0.9.0 (2024-10-03)

Features

  • Add support for three-way-comparison operator (spaceship operator)

Bugfixes

  • Fix handling mapping that omit key part
  • Fix documentation about twigAlwaysBreakObjects option to reflect actual default value
  • Fix autoescape block which allow boolean value (true and false) as a valid escape type

Internals

  • Make key part of ObjectProperty type optional to support object declaration that omit key part

0.8.0 (2024-08-09)

Features

  • Add support for arrow function inside filter, map, reduce filter

Internals

  • Optimize test runner by defining where to look for test files
  • NPM script alias to run prettier has been removed

0.7.0 (2024-08-01)

BREAKING CHANGES

  • Drop compatibility support for prettier 2
  • Plugin system has been removed from this project
  • Package has been renamed @zackad/prettier-plugin-twig-melody -> @zackad/prettier-plugin-twig
  • The parser has been renamed from melody into twig

Features

  • Add support attribute names according to html5 specs

Internals

  • Remove npm script to publish
  • Integrate devenv into nix flakes
  • Incorporate switch-plugin into core functionality
  • Replace jest with vitest as test runner

v0.6.0 (2024-03-27)

Features

  • BREAKING: Change package name prettier-plugin-twig-melody -> @zackad/prettier-plugin-twig-melody

Internals

  • Publish package as @zackad/prettier-plugin-twig-melody into npm registry

v0.5.0

Features

  • BREAKING: Drop compatibility with prettier lower than 2.3.0. Use version 0.4.6 if you need to use prettier older than 2.3.0
  • Fix compatibility with prettier 3

Internals

  • Add CI configuration for github actions
  • Add support for devenv.sh, nix based tools to manage development environment
  • Explicitly declare prettier configuration as yaml file
  • Fix eslint configuration to support ECMAScript 2017, allowing async function
  • Fix license name to use valid SPDX
  • Remove deprecated function usage of concat from prettier.doc.builders. Enforce this policy with eslint plugin eslint-plugin-prettier-doc using prettier-doc/no-concat rule.
  • Upgrade devDependencies to latest compatible version

v0.4.6

  • Bug fix: Leave parentheses in place in 'classA' ~ (not needsB ? ' classB')

v0.4.5

  • Bug fix (Melody): Whitespace trimming information was lost when using a conditional expression

v0.4.4

  • Bug fix: Expand the list of HTML tags treated as "inline"

v0.4.3

  • Bug fix: Filter expression was not correctly interpolated in a string

v0.4.2

  • Bug fix: Remove hard-coded space at the beginning of Twig comments

v0.4.1

  • Bug fix: Preserve leading (non-newline) whitespace in inline IF statement body

v0.4.0

  • Fix issue #1: Non-standard (custom) Twig tags don't make the parser crash any more, and receive at least some default printing. Complex tags can be configured using the twigMultiTags option.

v0.3.9

  • Fix issue #32: Curly braces around a binary expression were lost when they were in fact needed.

v0.3.8

  • Bug fix/optimization: ArrayExpression was causing too many newlines
  • Bug fix: Filter target was losing parentheses, which was altering the meaning (e.g., conditional expression)

v0.3.7

  • Bug fix: Expressions like not (a and b) lost the parentheses, thus changing the meaning of the expression.

v0.3.6

  • Bug fix: Optimize group structure when using SetStatement, VariableDeclarationStatement, and logical binary expressions. Previously, the group created by SetStatement had only one breaking point (before the closing %}), which led to some undesirable results.

v0.3.5

  • Fix: twigMelodyPlugins option is finally an array, not a string separated by | any more.

v0.3.4

  • Fix: Stylistic improvements

v0.3.3

  • Bug fix: Mount statement of the form mount ... from ... were dropping the source. Fixed now.

v0.3.2

  • Bug fix: No breaking point before closing %} for for, if, set.

v0.3.1

Bugfixes

  • Issue #24: Take the string contents into account when choosing the quote characters surrounding a string literal.

v0.3.0

Features

  • Issue #22: Add twigOutputEndblockName option so that you can choose whether to print the name in the {% endblock %} tag or not.

v0.2.3

Bugfixes

  • Issue #21: Always use double quotes around interpolated strings

v0.2.2

Bugfixes

  • Issue #20: Double quotation marks in include statement
  • Issue #21: Essentially same bug as Issue #20

v0.2.1

Bugfixes

  • Improve printing of HTML and Twig comments

v0.2.0

Features

  • Introduction of prettier-ignore functionality
  • Better adherence to Twig coding standards

Bugfixes


v0.1.1

Bugfixes


v0.1.0

Features

  • "If" statements can be in one line now, under certain circumstances
  • New option twigAlwaysBreakObjects
  • New option twigPrintWidth
  • All util functions are public now (available to plugins)
  • Optimizations: Fewer line breaks in a lot of cases, less indentation
  • Long HTML comments are re-wrapped now

Bugfixes

  • Respect operator precedence in binary expressions (especially boolean)

v0.0.34

Various small bug fixes and cosmetic optimizations


v0.0.33

Bugfixes

  • Empty block was causing an error. Does not an more.

v0.0.32

Features

  • Uses Melody 1.5.0
  • Trim left and trim right marks are preserved for Twig tags {%- ... -%}

v0.0.31

Features

  • Uses Melody 1.4.0
  • Whitespace trimming is now suppressed
  • Trim left and trim right marks are preserved for expressions {{- ... -}}

v0.0.30

Bugfixes

  • MacroDeclarationStatement: There was an extraneous space after the opening parenthesis
  • Plugin loading: project root path was not always correctly determined

v0.0.29

  • Make use of new melody-parser capabilities:
    • Add comment printing
    • Pass parser option to leave character entities undecoded
    • Preserve HTML comments

v0.0.28

  • Fix issue #2, where all final newlines in a file were skipped. Now, there will be one final newline.

v0.0.27

  • Fix attribute printing in objects. Computed attributes are now surrounded by (...). Keys that don't need quotes will not be quoted.