librelist archives

« back to archive

Loop variable in a filtered loop

Loop variable in a filtered loop

From:
Clodoaldo Neto
Date:
2012-04-20 @ 13:03
I want to filter a `for loop` over a `groupby` filter based on the `loop`
variable. This is what I'm doing:

    {% for group in list_of_dicts | groupby('attribute') -%}
        {% if loop.index < 9 %}
        ...
        {% endif %}
    {% endfor %}

In the docs there is this syntax:

    {% for user in users if not user.hidden %}
        <li>{{ user.username|e }}</li>
    {% endfor %}

How to use the above mentioned syntax when looping over a filter? I mean
like the following, which raises an `UndefinedError`:

    {% for group in list_of_dicts | groupby('attribute') if loop.index < 9
-%}
        {% if loop.index < 9 %}
        ...
        {% endif %}
    {% endfor %}

    UndefinedError: 'loop' is undefined. the filter section of a loop as
well as the else block don't have access to the special 'loop' variable of
the current loop.  Because there is no parent loop it's undefined.
Happened in loop on line 18 in 'page.html'

Regards, Clodoaldo

Re: Loop variable in a filtered loop

From:
Clodoaldo Neto
Date:
2012-04-22 @ 11:41
Em 20 de abril de 2012 10:03, Clodoaldo Neto <clodoaldo.pinto.neto@gmail.com
> escreveu:

> I want to filter a `for loop` over a `groupby` filter based on the `loop`
> variable. This is what I'm doing:
>
>     {% for group in list_of_dicts | groupby('attribute') -%}
>         {% if loop.index < 9 %}
>         ...
>         {% endif %}
>     {% endfor %}
>
> In the docs there is this syntax:
>
>     {% for user in users if not user.hidden %}
>         <li>{{ user.username|e }}</li>
>     {% endfor %}
>
> How to use the above mentioned syntax when looping over a filter? I mean
> like the following, which raises an `UndefinedError`:
>
>     {% for group in list_of_dicts | groupby('attribute') if loop.index < 9
> -%}
>         {% if loop.index < 9 %}
>         ...
>         {% endif %}
>     {% endfor %}
>
>     UndefinedError: 'loop' is undefined. the filter section of a loop as
> well as the else block don't have access to the special 'loop' variable of
> the current loop.  Because there is no parent loop it's undefined.
> Happened in loop on line 18 in 'page.html'
>
>
I had a good answer at StackOverflow[1]. It changes the approach from
filtering to slicing:

    {% for group in (list_of_dicts | groupby('attribute'))[:9] -%}
        ...
    {% endfor %}

The advantage is that in instead of looping over all the groups to filter
some, it will only loop over those I want.

[1]http://stackoverflow.com/a/10247085/131874

Regards, Clodoaldo
>

Re: [flask] Loop variable in a filtered loop

From:
Simon Sapin
Date:
2012-04-20 @ 17:36
Le 20/04/2012 15:03, Clodoaldo Neto a écrit :
> {% for group in ... if loop.index < 9 -%}
>      UndefinedError: 'loop' is undefined. the filter section of a loop
> as well as the else block don't have access to the special 'loop'
> variable of the current loop.

Hi,

The message looks pretty clear to me. The 'loop' variable is not 
available here. You need to use an explicit {% if %} tag:


{% for group in list_of_dicts | groupby('attribute') -%}
         {% if loop.index < 9 %}

Regards,
-- 
Simon Sapin