friendsoftwig/twigcs
Twigcs is a checkstyle/linter for Twig templates, like phpcs for PHP. Scan template directories for coding standard violations, control exit codes via severity, and exclude paths. Install via Composer or PHIVE and run as a CLI tool.
Installation
composer require --dev friendsoftwig/twigcs
Add to your project's composer.json under require-dev if not already present.
Basic CLI Usage Run TwigCS directly on a template file:
vendor/bin/twigcs path/to/template.twig
Or integrate into a CI pipeline (e.g., GitHub Actions) for automated checks.
First Use Case Validate a single Twig template for common issues (e.g., unused variables, deprecated tags):
vendor/bin/twigcs templates/partials/header.twig
Output will list violations with line numbers and severity.
CI/CD Pipeline
Add to composer.json scripts:
"scripts": {
"test": [
"@test-unit",
"twigcs --no-interaction templates/**/*.twig"
]
}
Fail builds on violations with --no-interaction (exit code 1 on errors).
Symfony 8.0+ Compatibility
With v6.6.1, TwigCS now supports Symfony 8.0's updated Console component. For Symfony projects, ensure your twigcs.yaml config remains unchanged:
twigcs:
paths: ["templates/"]
config: "%kernel.project_dir%/config/twigcs.json"
Run via Symfony CLI:
php bin/console twigcs:check
IDE/Editor Integration
twigcs as a custom linting tool in PHPStorm/VSCode via:
Settings > Languages & Frameworks > PHP > Code Sniffer (add custom tool).php-cs-fixer extension to run twigcs via settings.json.Custom Rulesets
Extend default rules by creating a twigcs.json config:
{
"rules": {
"TwigCS.Rules.UnusedVariable": {
"severity": "warning"
},
"TwigCS.Rules.DeprecatedTag": {
"severity": "error"
}
}
}
Run with:
vendor/bin/twigcs --config=path/to/twigcs.json templates/
Symfony 8.0 Console Compatibility
FatalError if not using v6.6.1+.composer update friendsoftwig/twigcs --dev
False Positives
UnusedVariable may flag variables used dynamically (e.g., {% if var in someArray %}).twigcs.json:
"TwigCS.Rules.UnusedVariable": {
"ignoreVariables": ["_context", "loop"]
}
Performance
**/*.twig) can be slow.--parallel for multi-core processing:
vendor/bin/twigcs --parallel templates/
Deprecated Tags
twigcs may flag tags as deprecated even if your Twig version supports them.--ignore-errors.--verbose to see skipped files/rules:
vendor/bin/twigcs --verbose templates/
vendor/bin/twigcs --dry-run
vendor/bin/twigcs --list-rules
Custom Rules
Extend TwigCS by creating a TwigCS\Rule\AbstractRule subclass. Example:
namespace App\TwigCS\Rules;
use TwigCS\Rule\AbstractRule;
class CustomRule extends AbstractRule {
public function getName() { return 'CustomRule'; }
public function getDescription() { return 'Checks for custom patterns.'; }
public function process(Node $node, TwigEnvironment $env) {
// Custom logic here
}
}
Register in twigcs.json:
"customRules": ["App\\TwigCS\\Rules\\CustomRule"]
Pre/Post-Processing Hook into TwigCS events via service providers (Symfony) or CLI arguments:
vendor/bin/twigcs --pre-process="App\\TwigCS\\PreProcessor"
Integration with PHP-CS-Fixer
Use easy-coding-standard to combine TwigCS with PHP-CS-Fixer:
composer require --dev easy-coding-standard
vendor/bin/ecs check src templates --config=ecs.php
Configure ecs.php:
return [
'rules' => [
\Squizlabs\PHP_Codesniffer\Ruleset::register('TwigCS'),
],
];
How can I help you explore Laravel packages today?