-
Hello, It's more about Symfony controller but as it directly impact twig usage, I feel it's a better place here. Currently in a template, when you pass arguments and it has the same name than a variable, you can save a bit: {{ path('app_replay_index', {event: event}) }}
{# ...can be compressed like... #}
{{ path('app_replay_index', {event}) }} It acts like in JS, so that's a nice trick to get the code more readable. return $this->render('template.html.twig', [
'sameAsVariable' => $sameAsVariable,
'not_same' => $foo,
]);
// .. Cannot be compressed like...
return $this->render('molecules/replay/search_preview.html.twig', [
$sameAsVariable,
'not_same' => $foo,
]); Not sure if it's hard technically or if levers some problems, but for behavior alignment purpose and for a nice DX improvement in controllers, I feel like it can be cool. Thank you for reading and sorry if it's naive! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is a feature request you would have to send to PHP, not to Twig, as you are asking about a different way to write a PHP associative array. But this would be rejected anyway, because |
Beta Was this translation helpful? Give feedback.
This is a feature request you would have to send to PHP, not to Twig, as you are asking about a different way to write a PHP associative array.
There is no way to emulate that when receiving an array that does not have string keys, as there is no way to know the name of the variable that was used when building the array (it has to be something done when parsing the code).
But this would be rejected anyway, because
[$sameAsVariable, 'not_same' => $foo]
is already valid PHP code that has a different behavior, and so it would be impossible to make sure RFC pass in PHP (Twig and JS were able to introduce this shortcut syntax because they have different delimiters{ }
vs[ ]
making it unambigu…