Installation Add the package via Composer (if still accessible):
composer require allocine/twigcs
Note: Since the package is archived, verify compatibility with your Twig/Laravel version.
Basic Usage Integrate TwigCS into your Laravel project by extending your existing Twig environment:
// In a service provider (e.g., AppServiceProvider)
$twig = $this->app['view.factory'];
$twig->getCompiler()->addExtension(new \Allocine\TwigCS\TwigCS());
First Use Case
Use TwigCS to validate a template file (e.g., resources/views/email.blade.php):
php artisan twigcs:check resources/views/email.blade.php
Outputs errors/warnings in a structured format.
CI/CD Pipeline Add TwigCS to your build process (e.g., GitHub Actions, GitLab CI):
# Example GitHub Actions step
- name: Run TwigCS
run: php artisan twigcs:check resources/views/
Pre-Commit Hooks
Use husky or pre-commit to run TwigCS before commits:
# .husky/pre-commit
php artisan twigcs:check resources/views/
Laravel Mix/Webpack
Integrate TwigCS with frontend tooling by running checks during npm run dev/build:
// webpack.mix.js
mix.scripts([...], 'public/js/app.js')
.then(() => {
require('child_process').execSync('php artisan twigcs:check resources/views/');
});
extends directives reference valid base templates:
{# resources/views/child.blade.php #}
@extends('layouts.app') {# TwigCS will flag if 'layouts.app' doesn't exist #}
{# TwigCS will warn if `$user` is not passed to the template #}
<p>Hello, {{ $user->name }}!</p>
False Positives
@extends($layout)) as invalid. Exclude such cases via config:
// config/twigcs.php (if available)
'ignore_dynamic_extends' => true,
@php blocks for dynamic includes if needed.Blade vs. Twig Confusion
@if in Twig templates) may cause errors.{% if %}) or use Blade directives consistently.Performance Overhead
php artisan twigcs:check resources/views/partials --cache
php artisan twigcs:check --verbose resources/views/
// app/Rules/NoHardcodedUrls.php
use Allocine\TwigCS\Rule\AbstractRule;
class NoHardcodedUrls extends AbstractRule {
public function check(Node $node) {
// Custom logic to detect hardcoded URLs
}
}
Note: Check the package’s Rule namespace for extension points.config/twigcs.php (if available):
'exclude' => [
'resources/views/vendor/*',
'resources/views/archived/*',
],
php artisan twigcs:check /path/to/custom/views --paths=/path/to/another
// app/Providers/TwigCSServiceProvider.php
$twigcs = new \Allocine\TwigCS\TwigCS();
$twigcs->setErrorFormatter(new CustomErrorFormatter());
// app/Providers/AppServiceProvider.php
Debugbar::extend('twigcs', function() {
return new TwigCSCollector();
});
How can I help you explore Laravel packages today?