Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Named argument support

Open kbond opened this issue 3 years ago • 23 comments

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.

  1. (probable bug) Filters/functions using is_variadic snake-cases the keys. This issue (and a possible solution) is explained fully in https://github.com/twigphp/Twig/issues/3475.
  2. Support in twig_get_attribute for PHP8+
    {{ object.method(prop1='value', prop2='value') }}
    
  3. Support in filters/functions without is_variadic for PHP8+
    {{ var|filter(prop1='value', prop2='value') }}
    
    {{ function(prop1='value', prop2='value') }}
    
  4. 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.

kbond avatar Jan 20 '22 23:01 kbond

~~Your link to the macro issue just links back to this issue.~~

Bilge avatar Jan 21 '22 13:01 Bilge

Thanks @Bilge, fixed.

kbond avatar Jan 21 '22 13:01 kbond

@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') }}

tacman avatar Oct 03 '22 12:10 tacman

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.

kbond avatar Oct 03 '22 13:10 kbond

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.

joeworkman avatar Apr 19 '23 19:04 joeworkman

Given that Twig 4 will require PHP 8.2+ (see #3881) named arguments support for macros could also be an option then.

xabbuh avatar Oct 10 '23 07:10 xabbuh