Well, I feel a little dumb, but let's make the most of it. Here's an excessively explicit walkthrough of how to enable additional Twig extensions for TwigBridge in Laravel.
TL;DR: Extensions are configured in extensions.php
, not config.php
.
Composer
Start here. Either use Composer's require CLI command; composer require twig/extensions
or add the following line to your composer.json file.
"twig/extensions": "1.2.*@dev",
If you modify composer.json, be sure to run composer update
so the package installs.
Configuring TwigBridge
To add Twig extensions to TwigBridge, they must be added to the enabled
array in laravel/app/config/packages/rcrowe/twigbridge/extensions.php
. Not config.php
. Even though there might be an extensions array in config.php
and it's where everything else is configured, that's not where extensions are enabled. This is spelled out in TwigBridge's documentation, but it's easy to overlook. I managed to miss it several times.
'enabled' => [
'TwigBridge\Extension\Loader\Facades',
'TwigBridge\Extension\Loader\Filters',
// ...
'Twig_Extensions_Extension_Text', // <- add this
],
TwigBridge's documentation refers to a config.php
file but the configuration file seems to have been renamed to twig.php
a while back. Generating a fresh configuration with artisan config:publish rcrowe/twigbridge
yielded twig.php
and extensions.php
files -- no config.php
. I've been using TwigBridge for a while so I still had one laying around, probably adding to my confusion.
Extension Names
The name of each extension can be found in the extension file--they're just Train_Case class names. As of January 2015, the Twig-extensions project contained five extensions, listed here with their included filters:
- Array -
Twig_Extensions_Extension_Array
filters: shuffle
- Date -
Twig_Extensions_Extension_Date
filters: time_diff
- I18n -
Twig_Extensions_Extension_I18n
filters: trans
- Intl -
Twig_Extensions_Extension_Intl
filters: localizeddate
, localizednumber
, localizedcurrency
- Text -
Twig_Extensions_Extension_Text
filters: truncate
, wordwrap