Twig
Twig copied to clipboard
Named argument support
As @weaverryan and I have been working on Twig/Live components in symfony/ux, we've come across a few areas where named argument support would be valuable. This issue is intended to track/discuss adding/improving support for this in twig.
- (probable bug) Filters/functions using
is_variadicsnake-cases the keys. This issue (and a possible solution) is explained fully in https://github.com/twigphp/Twig/issues/3475. - Support in
twig_get_attributefor PHP8+{{ object.method(prop1='value', prop2='value') }} - Support in filters/functions without
is_variadicfor PHP8+{{ var|filter(prop1='value', prop2='value') }} {{ function(prop1='value', prop2='value') }} - Could
:be used as an assignment operator instead of=?{{ var|filter(prop1: 'value', prop2: 'value') }} {{ function(prop1: 'value', prop2: 'value') }} {{ object.method(prop1: 'value', prop2:'value') }}
I understand there might be technical limitations that make supporting any/all of these impossible. This was the case with named argument support in macros.
~~Your link to the macro issue just links back to this issue.~~
Thanks @Bilge, fixed.
@kbond Any chance of seeing this before Symfony 6.2 goes into beta? As mentioned in a linked issue here, this format wouldn't break anything, and looks quite clean.
{{ foo(barValue: 'bar', booValue: 'boo') }}
Twig has different release schedule than Symfony so this can be added to twig in a feature release (anytime). There is a plan to migrate twig to Symfony core and I believe this is issue is on the radar.
I am creating a global that references a class. I would love to be able to have named parameters with my class functions. I assume that your suggested features would also cover that? This look perfect to me. I have a couple of functions that could potentially have a lot of parameters. It would be nice to be able to selectively only use parameters as I see fit.
Given that Twig 4 will require PHP 8.2+ (see #3881) named arguments support for macros could also be an option then.