core23/twig-extensions
Extra Twig extensions and helpers for PHP apps, packaged as a lightweight bundle. Adds convenient filters, functions, and runtime utilities to simplify templates and reduce boilerplate, with straightforward integration into existing Twig environments.
Installation Add the package via Composer in your Laravel project:
composer require core23/twig-extensions
Register the service provider in config/app.php:
'providers' => [
// ...
Core23\TwigExtensions\TwigExtensionsServiceProvider::class,
],
Basic Usage
Ensure Twig is configured in Laravel (e.g., via spatie/laravel-twig). Then, leverage the extensions in your Twig templates:
{{ "hello world"|uppercase }} {# Outputs: HELLO WORLD #}
First Use Case
Use the date filter to format dates elegantly:
{{ now()|date('Y-m-d') }} {# Outputs current date in YYYY-MM-DD format #}
String Manipulation Chain filters for concise transformations:
{{ " example " | trim | title | replace({' ': '-'}) }}
{# Outputs: "Example" #}
Conditional Rendering
Use if with custom filters for dynamic logic:
{% if user.isAdmin() %}
{{ "Admin Panel"|highlight("Admin") }}
{% endif %}
Loop Enhancements Add indices or keys to loops:
{% for item in items %}
{{ loop.index }}. {{ item.name }}
{% endfor %}
JSON Handling Pretty-print JSON in templates:
{{ json_encode(data) | json_pretty }}
{{ Twig\... }} or embed Twig templates.use Core23\TwigExtensions\TwigExtensionsServiceProvider;
class CustomTwigExtensionsServiceProvider extends TwigExtensionsServiceProvider {
public function registerExtensions() {
$this->twig->addExtension(new \Custom\Twig\Extension());
}
}
config/twig.php for performance:
'cache' => env('APP_ENV') !== 'production' ? false : storage_path('framework/views'),
Filter Conflicts
Avoid naming custom filters the same as built-in Twig/this package’s filters (e.g., date). Prefix them:
{{ now()|myapp_date('custom_format') }}
Performance with Loops
Heavy loops with loop filters (e.g., loop.index, loop.length) can slow rendering. Cache results if used frequently:
{% set loopData = loop %}
{% for item in items %}
{{ loopData.index }}. {{ item.name }}
{% endfor %}
JSON Security
The json_encode filter escapes output by default. Use |raw cautiously:
{{ json_encode(data)|raw }} {# Only if data is trusted #}
Filter Not Working?
Verify the extension is registered. Check bootstrap/cache/services.php for the provider.
php artisan config:clear
php artisan cache:clear
Twig Errors
Enable Twig’s debug mode in config/twig.php:
'debug' => env('APP_DEBUG', false),
Custom Filters Add filters via a service provider:
$this->twig->addFilter(new \Twig\TwigFilter('custom_filter', function ($str) {
return strtoupper($str);
}));
Global Variables Pass data to all Twig templates:
TwigExtensionsServiceProvider::booting(function () {
$twig = app('twig');
$twig->addGlobal('app_name', config('app.name'));
});
Override Existing Filters
Replace default filters (e.g., date) by re-registering them with custom logic:
$this->twig->addFilter(new \Twig\TwigFilter('date', function ($date, $format) {
return Carbon::parse($date)->format($format);
}));
How can I help you explore Laravel packages today?