vincentlanglet/twig-cs-fixer
A coding standards fixer for Twig templates. Analyze and automatically format Twig files with consistent style rules, configurable presets, and CI-friendly checks to keep templates clean and readable across your project.
Installation:
composer require --dev vincentlanglet/twig-cs-fixer
Ensure bin-dependencies is enabled in your composer.json:
"config": {
"bin-dir": "bin",
"bin-dependencies": true
}
First Run (Linting):
vendor/bin/twig-cs-fixer lint resources/views
This checks Twig files in resources/views for coding standard violations.
First Fix:
vendor/bin/twig-cs-fixer fix resources/views
Automatically applies fixes to non-compliant files.
.twig-cs-fixer.php in your project root to customize rules (see Implementation Patterns).vendor/bin/twig-cs-fixer --help
Lists all available commands, reporters, and options.Integrate twig-cs-fixer into your workflow to enforce consistency before commits:
# Add to package.json (if using Laravel Mix)
"scripts": {
"lint:twig": "twig-cs-fixer lint resources/views"
}
Or use a tool like Husky to run it automatically.
.twig-cs-fixer.php:
<?php
$ruleset = new \TwigCsFixer\Ruleset\Ruleset();
$ruleset->addStandard(new \TwigCsFixer\Standard\TwigCsFixer());
$ruleset->overrideRule(new \TwigCsFixer\Rules\Whitespace\EmptyLinesRule(2)); // 2 lines between blocks
$config = new \TwigCsFixer\Config\Config();
$config->setRuleset($ruleset);
return $config;
vendor/bin/twig-cs-fixer fix resources/views --config=.twig-cs-fixer.php
SingleQuoteRule for legacy projects:
$ruleset->removeRule(\TwigCsFixer\Rules\String\SingleQuoteRule::class);
.twig-cs-fixer.dist.php for team defaults and .twig-cs-fixer.php for overrides.- name: Lint Twig
run: vendor/bin/twig-cs-fixer lint resources/views --report=github
Outputs annotations for failed checks.vendor/bin/twig-cs-fixer fix resources/views/partials --dry-run
Use --dry-run to preview changes.// app/Console/Commands/FixTwigCommand.php
namespace App\Console\Commands;
use TwigCsFixer\Application;
class FixTwigCommand extends Command {
protected $signature = 'twig:fix';
public function handle() {
$app = new Application();
$app->run(['fix', 'resources/views']);
}
}
Add to app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\FixTwigCommand::class,
];
Run with:
php artisan twig:fix
External Tools settings to run twig-cs-fixer on save:
vendor/bin/twig-cs-fixerfix $FilePath$$ProjectFileDir$AbstractRule:
namespace App\Rules;
use TwigCsFixer\Rule\AbstractRule;
class CustomBlockRule extends AbstractRule {
public function getName() {
return 'custom_block_rule';
}
public function process(\TwigCsFixer\File $file) {
// Custom logic
}
}
Register in .twig-cs-fixer.php:
$ruleset->addRule(new \App\Rules\CustomBlockRule());
Cache Issues:
--no-cache or set null in config:
$config->setCacheFile(null);
Non-Fixable Rules:
FileNameRule are non-fixable by default and may cause lint failures without fixes.$config->allowNonFixableRules();
Twig Version Mismatch:
composer require twig/twig:^3.12
Overly Strict Rules:
SingleQuoteRule may break legacy templates with escaped single quotes.$ruleset->overrideRule(new \TwigCsFixer\Rules\String\SingleQuoteRule([
'skipStringContainingSingleQuote' => false,
]));
Performance:
vendor/bin/twig-cs-fixer lint resources/views --no-cache
Dry Runs:
Use --dry-run to preview changes without modifying files:
vendor/bin/twig-cs-fixer fix --dry-run
Verbose Output: Enable debug mode for detailed logs:
vendor/bin/twig-cs-fixer lint --verbose
Rule-Specific Debugging: Isolate rule failures by testing individual rules:
vendor/bin/twig-cs-fixer lint --rules=DelimiterSpacingRule
Custom Reporter:
Use junit or github reporters for CI debugging:
vendor/bin/twig-cs-fixer lint --report=junit > report.xml
Partial Fixes:
Use --path-mode=union to fix only files that violate rules:
vendor/bin/twig-cs-fixer fix --path-mode=union
Exclude Files:
Configure the Finder to skip specific files/directories:
$finder = new \TwigCsFixer\File\Finder();
$finder->in('resources/views')->exclude(['legacy', 'vendor']);
$config->setFinder($finder);
Symfony Integration:
Combine with symfony/coding-standard for PHP/Twig consistency:
composer require --dev symfony/coding-standard
vendor/bin/ecs check src resources/views
Custom Token Parsers: Extend Twig’s token parsing for custom syntax (e.g., Laravel Blade-like directives):
$config->addTokenParser(new \App\Twig\CustomTokenParser());
Git Pre-Commit:
Use twig-cs-fixer in a pre-commit hook via PHP-CS-Fixer’s pre-commit example:
# .git/hooks/pre-commit
#!/bin/sh
vendor/bin/twig-cs-fixer fix resources/views --allow-risky=yes
IDE Formatting:
Configure your IDE to trigger twig-cs-fixer on save (e.g., PHPStorm’s onSave actions).
**Leg
How can I help you explore Laravel packages today?