twig/extra-bundle
Symfony bundle that auto-enables all Twig “extra” extensions with zero configuration. Install via Composer (twig/extra-bundle) to add optional Twig features to your app quickly, keeping setup minimal and consistent across environments.
Installation:
composer require twig/extra-bundle
Register the bundle in config/bundles.php (Symfony 4.3+ auto-discovers it).
First Use Case:
Enable the Twig_ExtraBundle in config/packages/twig.yaml (if not auto-loaded):
twig:
extra_bundle:
enabled: true
Verify by using a built-in extension in a Twig template:
{{ 'hello'|upper }}
(Renders HELLO due to the StringExtension.)
Key Files to Check:
config/packages/twig.yaml (for bundle configuration).templates/ (for testing extensions in templates).var/cache/dev/ (for debugging compiled Twig).String Manipulation:
Use StringExtension for common text operations:
{{ 'user@example.com'|email_address }} {# Formats as "User <user@example.com>" #}
{{ '2023-10-01'|date('Y-m-d H:i') }} {# Converts to full datetime #}
URL Generation:
Leverage UrlExtension for dynamic links:
{{ path('app_home') }} {# Generates route URL #}
{{ url('https://example.com')|absolute_url }} {# Converts to absolute URL #}
Form Handling:
Use FormExtension for Twig forms (if using Symfony Forms):
{{ form_start(form) }}
{{ form_widget(form.username) }}
{{ form_errors(form) }}
{{ form_end(form) }}
Integration with Laravel:
twig-laravel/twig to bridge Twig with Laravel’s templating.$this->app->singleton(Twig_Environment::class, function ($app) {
$loader = new \Twig\Loader\FilesystemLoader($app['path.base'].'/resources/views');
return new Twig_Environment($loader, [
'cache' => $app['path.cache'].'/twig',
'debug' => $app->environment() === 'local',
]);
});
$twig->addGlobal('auth', $app['auth']->user());
Custom Extensions: Extend Twig’s functionality by creating custom extensions:
// app/Extensions/CustomExtension.php
class CustomExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('greet', [$this, 'greetUser']),
];
}
public function greetUser($name)
{
return "Hello, $name!";
}
}
Register in config/packages/twig.yaml:
twig:
extensions:
- App\Extensions\CustomExtension
Caching Issues:
php bin/console cache:clear
debug: true in twig.yaml) bypasses caching but slows development.Namespace Conflicts:
upper).app_greet).Symfony vs. Laravel Quirks:
path() with Laravel’s route names:
{{ path('home') }} {# Laravel route #}
asset() helper in Twig:
<img src="{{ asset('images/logo.png') }}">
Deprecation Warnings:
twig/extra-bundle is a wrapper for twig/extra. Ensure compatibility with your Twig version (e.g., Twig 3.x for Symfony 5.4+).Enable Twig Debugging:
twig:
debug: true
strict_variables: true {# Catch undefined variables #}
View errors in browser or Symfony’s profiler.
Log Twig Output: Use Symfony’s logger to dump Twig variables:
{{ dump(_context) }} {# Symfony-only; use `var_dump()` in Laravel #}
Extension Loading Order:
Extensions are loaded in alphabetical order. Explicitly order them in twig.yaml if dependencies exist:
twig:
extensions:
- App\Extensions\FirstExtension
- App\Extensions\SecondExtension
Override Built-in Extensions: Disable default extensions and provide custom ones:
twig:
extra_bundle:
enabled: false
Then register only the extensions you need:
twig:
extensions:
- Twig\Extensions\StringExtension
- App\Extensions\CustomExtension
Add Custom Filters/Functions: Extend existing extensions without rewriting them:
class ExtendedStringExtension extends \Twig\Extensions\StringExtension
{
public function getFilters()
{
return array_merge(
parent::getFilters(),
[
new \Twig\TwigFilter('custom_upper', [$this, 'customUpper']),
]
);
}
public function customUpper($string)
{
return strtoupper($string) . " (custom)";
}
}
Leverage Symfony’s Event System:
Hook into Twig events (e.g., Twig\EventDispatcher\EnvironmentEvents::PRE_RENDER) to modify output dynamically.
How can I help you explore Laravel packages today?