I'm using twig with laravel using TwigBridge. I'd like to register a custom function to use from within twig templates.
The documents states that extensions can be added via the extension-array, but this isn't clear to me.
What extension array is the documentation referring to?
The process I followed was:
Create my filters class at app/extensions/twig/TwigFilters.php:
namespace App\Extensions\Twig;
class TwigFilters extends \Twig_Extension {
    //... filters implementation
}
Add the filter folder to composer.json autoload > classmap
"autoload": {
    "classmap": [
        ...
        "app/extensions/twig",
        ...
    ]
},
Update autoloader: php composer.phar dump-autoload
Create the TwigBridge config at app/config/packages/rcrowe/twigbridge/config.php:
php artisan config:publish rcrowe/twigbridge
Edit the extensions key on the previously created config.php:
'extensions' => array(
    ...
    'App\Extensions\Twig\TwigFilters',
    ...
)
All filters defined in that class are already available in the Twig views.
Additional Info:
What is referring is to this array in the config file. You should publish the config to your app/ using php artisan config:publish rcrowe/twigbridge and then edit that array. As you can see in the same config file you can also add alias.
You can create a class with your custom functions extending \TwigBridge\Extension and then add it to your config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With