umpirsky/twig-php-function
Lightweight Twig extension that lets you call any PHP function directly from your templates with a simple php_function() call. Skip writing custom Twig extensions for quick formatting, hashing, or utility helpers. Works with Twig 2 and 3.
composer require umpirsky/twig-php-function
config/twigbridge.php under the extensions key:
'extensions' => [
// ...
Umpirsky\Twig\Extension\PhpFunctionExtension::class,
],
Then clear config cache: php artisan config:clear.$twig->addExtension(new \Umpirsky\Twig\Extension\PhpFunctionExtension());
php. First use case: quick formatting in a template without writing a custom filter:
{{ php('ucwords', user.name) }}
{{ php('number_format', product.price, 2) }}
{{ php('date', 'Y-m-d', order.createdAt.timestamp) }}
vendor/umpirsky/twig-php-function/src/PhpFunctionExtension.php. The README is minimal; the source is small and tells you exactly how arguments are forwarded and what exceptions are thrown.php() for simple string, array, or number transformations where a dedicated Twig filter feels heavy.
{{ php('array_slice', items, 0, 3) | join(', ') }}
call_user_func_array supports Class::method, so you can route complex logic to a static helper and keep templates readable.
{{ php('App\\Support\\Format::currency', amount, currency) }}
{% set slug = php('strtolower', php('str_replace', ' ', '-', title)) %}
sprintf and similar functions work naturally.
{{ php('sprintf', '%s (%s)', name, code) }}
{% if %} for checks Twig lacks.
{% if php('in_array', role, ['admin', 'owner']) %}
For repeated checks, prefer a custom Twig test.php() is available in every .twig file. No manual filter registration is needed unless you want a different function name.php() as a bridge for edge cases. For common operations (dates, money, slugs), write a dedicated Twig filter or function so you get a stable, testable API and better security.exec, system, file_put_contents, etc.). To restrict the surface, subclass PhpFunctionExtension and override call() with an allow-list.nl2br), you must add |raw, which reintroduces XSS risk. Prefer functions that return plain text or escape manually.var_dump, print_r, and printf write to output and return null/true. The side-effect output appears before the escaped return value, potentially breaking your layout. Avoid them or wrap them in output buffering inside a helper.call_user_func_array does not preserve references for parameters like the third argument of preg_match or the array in sort. Calls may fail or behave unexpectedly. Test before relying on them.disable_functions, the extension throws a RuntimeException. Verify with php -r 'var_dump(function_exists("your_function"));' and check php.ini.Twig\Extension\AbstractExtension. If not, you may need a shim or an updated fork.php artisan config:clear (and view:clear if needed). If php() is undefined, the extension isn't registered or the config is cached.function_exists check and a call_user_func_array. Negligible for typical templates, but avoid calling inside tight loops over large collections; precompute in the controller or a view composer.safe_php) that only allows a curated list. You can also register it conditionally based on the environment.null or outputs directly. With Twig debug enabled, use {{ dump(php('strtoupper', 'test')) }} to inspect the return value.How can I help you explore Laravel packages today?