Skip to content

Commit

Permalink
feature #4518 Add invoke filter (Lorenz, Lorenzschaef, fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Add invoke filter

Adds a new filter `invoke`, to invoke arrow functions and other php callables.

See the discussion here: #4378 (comment)

Commits
-------

3e93e91 Finish the work
c4fd25f fix indentation
dacbe51 Apply suggestions from code review
2b95bd6 typo
09c30c3 fix merge
d58ac9a docs and changelog
edecc13 typehint instead of checkArrow
f271158 checkArrow, typehints
8b43275 invoke filter
  • Loading branch information
fabpot committed Jan 12, 2025
2 parents 9d1397a + 3e93e91 commit 65fa0c6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.19.0 (2025-XX-XX)

* Add the `invoke` filter
* Make `{}` optional for the `types` tag
* Add `LastModifiedExtensionInterface` and implementation in `AbstractExtension` to track modification of runtime classes

Expand Down
16 changes: 16 additions & 0 deletions doc/filters/invoke.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
``invoke``
==========

.. versionadded:: 3.19

The ``invoke`` filter has been added in Twig 3.19.

The ``invoke`` filter invokes an arrow function with the given arguments:

.. code-block:: twig
{% set person = { first: "Bob", last: "Smith" } %}
{% set func = p => "#{p.first} #{p.last}" %}
{{ func|invoke(person) }}
{# outputs Bob Smith #}
7 changes: 7 additions & 0 deletions doc/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,13 @@ The following operators don't fit into any of the other categories:
Arrow function support for functions, macros, and method calls was added in
Twig 3.15 (filters and tests were already supported).

Arrow functions can be called using the :doc:`invoke </filters/invoke>`
filter.

.. versionadded:: 3.19

The ``invoke`` filter has been added in Twig 3.19.

Operators
~~~~~~~~~

Expand Down
11 changes: 11 additions & 0 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public function getFilters(): array
// iteration and runtime
new TwigFilter('default', [self::class, 'default'], ['node_class' => DefaultFilter::class]),
new TwigFilter('keys', [self::class, 'keys']),
new TwigFilter('invoke', [self::class, 'invoke']),
];
}

Expand Down Expand Up @@ -915,6 +916,16 @@ public static function keys($array): array
return array_keys($array);
}

/**
* Invokes a callable
*
* @internal
*/
public static function invoke(\Closure $arrow, ...$arguments): mixed
{
return $arrow(...$arguments);
}

/**
* Reverses a variable.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/filters/invoke.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
"invoke" filter
--TEMPLATE--
{% set func = x => 'Hello '~x %}
{{ func|invoke('World') }}
{% set func2 = (x, y) => x+y %}
{{ func2|invoke(3, 2) }}
--DATA--
return []
--CONFIG--
return []
--EXPECT--
Hello World
5

0 comments on commit 65fa0c6

Please sign in to comment.