How to use custom twig functions in laravel using TwigBridge

I am using the laravel branch using TwigBridge . I would like to register a custom function to use from branch templates.

The docs say that extensions can be added through an array of extensions, but this is not clear to me.

What array of extensions is related documentation?

+4
source share
2 answers

What does this array mean in the configuration file. You have to publish the configuration in your application / using php artisan config:publish rcrowe/twigbridge and then edit this array. As you can see in the same configuration file, you can also add an alias.

You can create a class with your custom functions by extending \TwigBridge\Extension , and then add it to your configuration.

+2
source

The next process I was:

  • Create a class of my filters in app/extensions/twig/TwigFilters.php :

     namespace App\Extensions\Twig; class TwigFilters extends \Twig_Extension { //... filters implementation } 
  • Add filter folder in composer.json autoload > classmap

     "autoload": { "classmap": [ ... "app/extensions/twig", ... ] }, 
  • Autoloader update: php composer.phar dump-autoload

  • Create a TwigBridge configuration in app/config/packages/rcrowe/twigbridge/config.php :

     php artisan config:publish rcrowe/twigbridge 
  • Edit the extensions key on the previously created config.php file:

     'extensions' => array( ... 'App\Extensions\Twig\TwigFilters', ... ) 
  • All filters defined in this class are already available in Twig views.

Additional Information:

+14
source

Source: https://habr.com/ru/post/1479505/


All Articles