ravenflux / php-functions
Twig extension that implements a way to use native PHP functions and filters in Twig template
dev-master / 3.x-dev
2021-01-02 23:10 UTC
Requires
- php: >=8.0.0
- jetbrains/phpstorm-attributes: *
- twig/twig: 3.*
- vairogs/utils: 3.x-dev
This package is auto-updated.
Last update: 2021-03-31 00:05:00 UTC
README
Twig extension that implements a way to use native PHP functions and filters in Twig template.
Medium article
Installation
The recommended way to install is via Composer:
composer require ravenflux/php-functions
This package requires at least PHP 8.0.0
For Symfony usage add it as a service and tag it:
# config/services.yaml services: ravenflux.twig.extension.php_functions: class: RavenFlux\Php\PhpFunctionsExtension arguments: - #first argument are functions - 'count' - #second argument are filters - 'nl2br' tags: - name: twig.extension
or without any arguments if you want to use dynamic function / filters:
# config/services.yaml services: ravenflux.twig.extension.php_functions: class: RavenFlux\Php\PhpFunctionsExtension tags: - name: twig.extension
Function VS Filter | source
A function is used when you need to compute things to render the result.
A filter is a way to transform the displayed data.
Usage
Functions:
{{ count(users) }}
Filters:
{{ user.name|nl2br }}
or dynamically any php function:
Functions:
{{ raven_function('count', users) }}
Filters:
{{ user.name|raven_filter('nl2br') }}