Twig CS Fixer
Installation
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
Twig Coding Standard Rules
From the official one.
Delimiter spacing
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.
Operator spacing
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 ..
.
Punctuation spacing
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.
Custom configuration
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,
$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