This standard can be installed with the Composer dependency manager.
Add the coding standard as a dependency of your project
composer require --dev vincentlanglet/twig-cs-fixer
Then, use it!
bin/twig-cs-fixer lint /path/to/code
bin/twig-cs-fixer lint --fix /path/to/code
From the official one.
Put one (and only one) space after the start of a delimiter ({{
, {%
, and {#
)
and before the end of a delimiter (}}
, %}
, and #}
).
When using the whitespace control character, do not put any spaces between it and the delimiter.
Put one (and only one) space before and after the following operators:
comparison operators (==
, !=
, <
, >
, >=
, <=
), math operators (+
, -
, /
, *
, %
, //
, **
),
logic operators (not
, and
, or
), ~
, is
, in
, and the ternary operator (?:
).
Do not put any spaces before and after the operator ..
.
Put one (and only one) space after the :
sign in hashes and ,
in arrays and hashes.
Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions.
Do not put any spaces before and after the following operators: |
, .
, []
.
Do not put any spaces before and after the parenthesis used for filter and function calls.
Do not put any spaces before and after the opening and the closing of arrays and hashes.
By default, the generic standard is enabled with the twig coding standard rules and the following sniffs:
BlankEOFSniff
: Ensure that files ends with one blank line.EmptyLinesSniff
: Checks that there are not 2 empty lines following each other.TrailingCommaSingleLineSniff
: Ensure that single-line arrays, objects and arguments list does not have a trailing comma.TrailingSpaceSniff
: Ensure that files has no trailing spaces.
If you want to use a custom standard and/or add/disable a sniff, you can provide your own configuration with
a .twig-cs-fixer.php
file which returns a TwigCsFixer\Config\Config
class. For instance,
<?php
$ruleset = new TwigCsFixer\Ruleset\Ruleset();
$ruleset->addStandard(new TwigCsFixer\Standard\Generic());
$ruleset->removeSniff(TwigCsFixer\Sniff\EmptyLinesSniff::class);
$config = new TwigCsFixer\Config\Config();
$config->setRuleset($ruleset);
return $config;
If your config is not located in your current directory, you can pass his path when running the command.
bin/twig-cs-fixer lint --config=dir/.twig-cs-fixer.php /path/to/code
By default, all the .twig
files in the current directory are linted, except the one in the vendor
directory.
If you want to lint a specific files/directory you can pass it as argument, but if you want a more sophisticated
rule, you can configure it in the .twig-cs-fixer.php
file. For instance,
<?php
$finder = new TwigCsFixer\File\Finder();
$finder->exclude('myCustomDirectory');
$config = new TwigCsFixer\Config\Config();
$config->setFinder($finder);
return $config;