librelist archives

« back to archive

[flask] Multiple For loops in Jinja

[flask] Multiple For loops in Jinja

From:
Delilah Parks
Date:
2012-06-29 @ 03:07
*Multiple For loops in Jinja:*

This following works... but it is only looping the same content over the
length of x:

{% for x in g.pre_order|getnumbers %}
 {{ x }}
 {% for t in gPreQues %}
 {{ t.pre_questions.0 }}
 {% endfor %}
{% endfor %}

However, I am trying to perform a loop to populate the proper order ... so
the output I assumed to be the following:

 {{ t.pre_questions.x }}     #doesn't work

I am trying to use the x variable from the first "for loop" in the second
"for loop".... how would I do this in Jinja. PLEASE HELP! Or, is there an
alternative way of doing this?

Thanks all!

Re: [flask] Multiple For loops in Jinja

From:
Delilah Parks
Date:
2012-06-29 @ 04:29
Nevermind.... I figured out... I forgot to convert the type

{{ t.pre_questions[x|int] }} :)

On Thu, Jun 28, 2012 at 11:07 PM, Delilah Parks <delilahtp@gmail.com> wrote:

> *Multiple For loops in Jinja:*
>
> This following works... but it is only looping the same content over the
> length of x:
>
> {% for x in g.pre_order|getnumbers %}
>  {{ x }}
>   {% for t in gPreQues %}
>  {{ t.pre_questions.0 }}
>  {% endfor %}
> {% endfor %}
>
> However, I am trying to perform a loop to populate the proper order ... so
> the output I assumed to be the following:
>
>  {{ t.pre_questions.x }}     #doesn't work
>
> I am trying to use the x variable from the first "for loop" in the second
> "for loop".... how would I do this in Jinja. PLEASE HELP! Or, is there an
> alternative way of doing this?
>
> Thanks all!
>

Re: [flask] Multiple For loops in Jinja

From:
pronoyc@gmail.com
Date:
2012-06-29 @ 05:06
I would like to add to her question. Multiple for loops for the same key
don't more than once.

@app.route('/')
def index():
"""index function"""
 foo = some_data()
return render_template('index.html', foo=foo)
 {% for i in foo %}
{{ i.value }}
{% endfor %}
<p>Html and other stuff here</p>
{% for stuff in foo %}
<p>{{ stuff.value }}</p>
{% endfor %}

As stated above, running a loop on the same key doesn't work. It gives a
blank. Can anyone explain why this happens and what should be done to avoid
such failure.

Oh, another thing, having passed foo twice in context under a different
name doesn't work either. It gets enumerated once only, the first time.

-- 
Regards,

Pronoy Chopra
http://blog.pronoy.in <http://www.pronoy.in/about>/
http://www.twitter.com/pronoyc

Re: [flask] Multiple For loops in Jinja

From:
Simon Sapin
Date:
2012-06-29 @ 06:19
Le 29/06/2012 07:06, pronoyc@gmail.com a écrit :
> foo = some_data()
>
> As stated above, running a loop on the same key doesn't work. It gives a
> blank. Can anyone explain why this happens and what should be done to
> avoid such failure.
>
> Oh, another thing, having passed foo twice in context under a different
> name doesn't work either. It gets enumerated once only, the first time.

Hi,

It depends on what is some_data. If it is a generator[1], the items are 
generated one at a time when you consume them without having all of them 
in memory. Once you have exhausted a generator it will behave like an 
empty sequence. If you only consume it partly (eg. stopping with a 
'break' statement) it will start where it left off when you consume it 
again.

[1] http://docs.python.org/glossary.html#term-generator

This has nothing to do with Jinja: you would see the same with Python loops.

If you want to keep everything in memory you can explicitly make a list:

foo = list(some_data())

Regards,
-- 
Simon Sapin

Re: [flask] Multiple For loops in Jinja

From:
pronoyc@gmail.com
Date:
2012-06-29 @ 07:09
>
> It depends on what is some_data. If it is a generator[1], the items are
> generated one at a time when you consume them without having all of them
> in memory. Once you have exhausted a generator it will behave like an
> empty sequence. If you only consume it partly (eg. stopping with a
> 'break' statement) it will start where it left off when you consume it
> again.
>
> [1] http://docs.python.org/glossary.html#term-generator



Simon, thanks, that was it. I was retrieving data from pymongo and
outputting it into the template. This will fix it, thanks again.



-- 
Regards,

Pronoy Chopra
http://blog.pronoy.in <http://www.pronoy.in/about>/
http://www.twitter.com/pronoyc