twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.17k stars 1.25k forks source link

Passing a numeric variable name rewrites the variable name as "0" #3490

Closed Bilge closed 1 month ago

Bilge commented 3 years ago
{{ include('foo.twig', {123: true}) }}
{# OR #}
{{ include('foo.twig', {"123": true}) }}

{# foo.twig #}
{{ dump(_context) }}

array:1 [▼ 0 => true ]

stof commented 3 years ago

Variables names starting with a number are not supported. You won't be able to use such variables normally (i.e. without going through the special _context variable), as their names are not valid identifiers.

So to me, that's a won't fix. But I'll let @fabpot give its opinion there.

Bilge commented 3 years ago

It's also not an error. If it's not supported, it should be clear, instead of exhibiting weird behaviour.

stof commented 3 years ago

Well, checking all array keys when passing variables to another template will have a performance impact.

but maybe some of these cases could be solved by replacing a few array_merge with array_replace in the codebase.

fabpot commented 1 month ago

I've tried it today and it works well. I've also tried with very old versions of Twig and it works. So, I'm missing something here, but I don't know what.