Skip to content

Update for.rst - #4862

Merged
fabpot merged 2 commits into
twigphp:3.xfrom
marilenaRM:patch-1
Jul 21, 2026
Merged

Update for.rst#4862
fabpot merged 2 commits into
twigphp:3.xfrom
marilenaRM:patch-1

Conversation

@marilenaRM

Copy link
Copy Markdown
Contributor

Add context and explanation about loop.parent.

Add context and explanation about loop.parent
Comment thread doc/tags/for.rst Outdated
Comment on lines +83 to +84
When loops are nested, you can access the parent loop context via the
``parent`` key of the ``loop`` variable:

@homersimpsons homersimpsons Jul 20, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, this is not only on nested loop:

One use-case I found when documenting this in #4865 is about variable shadowing:

{% set users = [{"name": "Homer"}] %}
{% set user = {'name': 'Fabien'} %}
{% for user in users %}
    {{ loop.parent.user.name }} - {{ user.name }}
{% endfor %}

I do not have a proper wording to document this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — loop.parent is indeed the whole enclosing context, not just nested loops. I've reworded the intro and added your variable-shadowing example. Thanks!

@marilenaRM
marilenaRM requested review from homersimpsons and stof July 20, 2026 16:59
@fabpot

fabpot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Thank you @marilenaRM.

@fabpot
fabpot merged commit 7f94646 into twigphp:3.x Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants