Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing outer for loop #500

Closed
rohitpaulk opened this issue Dec 17, 2014 · 2 comments · Fixed by #520
Closed

Accessing outer for loop #500

rohitpaulk opened this issue Dec 17, 2014 · 2 comments · Fixed by #520

Comments

@rohitpaulk
Copy link

I can't seem to find a way to access the forloop object for the outermost for loop in a template that looks like this.

{% for outerItem in outerItems %}
    {% for item in items%}
        <div>{{ forloop.counter }}.&nbsp;{{ item }}</div>
    {% endfor %}
{% endfor %}

forloop.counter returns the innermost for loop's counter in the above example

@nickpearson
Copy link
Contributor

You can assign the outer forloop to a variable before it gets shadowed by the inner forloop variable:

{% for outerItem in outerItems %}
    {% assign outer_forloop = forloop %}
    {% for item in items%}
        <div>{{ outer_forloop.counter }}.&nbsp;{{ item }}</div>
    {% endfor %}
{% endfor %}

@rohitpaulk
Copy link
Author

Yep, that should work! Thanks @nickpearson!

Looks like Django's templating language has a forloop.parentloop variable attached to it, that can be used to refer to the outer loop directly. I'm leaving this issue open for people to leave their comments if they'd be interested in something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants