twigphp / Twig

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

Is the `_parent` value present in the context supposed to be iterated on by the `for` tag? #4152

Closed ericmorand closed 3 months ago

ericmorand commented 3 months ago

This is related to https://github.com/twigphp/Twig/issues/1584.

When iterating on the context, one of the iterated element is the parent context, identifier by the _parent key. TwigFiddle being down, here is a quick Twig sample:

{% for key, value in _context %}
  {{ key }}
{% endfor %}

The output of this template, with an empty context, is:

  _parent

Is it something that is contractual per TwigPHP specification? If not, why is it there, and why do developers have to know that it exists in order to not take it into account, like in the mentioned issue?

fabpot commented 3 months ago

The _parent item is purely internal stuff that only exists in the context of a loop and is not guaranteed to exist. It might disappear in 4.0.

fabpot commented 3 months ago

See #4153

ericmorand commented 3 months ago

Very good news. Thank you.