librelist archives

« back to archive

Is there a way to retrieve the value of a variable set within a for loop?

Is there a way to retrieve the value of a variable set within a for loop?

From:
Ben Michie
Date:
2012-07-16 @ 09:40
Hello! Is there a way to retrieve the value of a variable set within a for
loop? The way I thought it would work is as follows (items is a passed in
list [1,2,3]):

{% set outer_scope_variable = 0 %}
{% for item in items %}
<p>inside for: {{ outer_scope_variable }}</p>
{% set outer_scope_variable = outer_scope_variable + 1 %}
{% endfor %}
<p>outside for: {{ outer_scope_variable }}</p>


This loops over the for-loop three times (because the list is 3 items long)
and increments the variable each time. I expected that using the variable
outside the for-loop, after the for-loop, would return the incremented
value (2). Instead it returns the original value (0).

Thanks!

Re: [flask] Is there a way to retrieve the value of a variable set within a for loop?

From:
Redouane Zait
Date:
2012-07-16 @ 10:33
you don't even need that, if the value you're looking for is the number of
the items in list (number of iterations) you could try {{ items|length }}
or else, check this out

http://stackoverflow.com/questions/4870346/can-a-jinja-variables-scope-extend-beyond-in-an-inner-block

On Mon, Jul 16, 2012 at 10:40 AM, Ben Michie <ben@tinku.net> wrote:

> Hello! Is there a way to retrieve the value of a variable set within a for
> loop? The way I thought it would work is as follows (items is a passed in
> list [1,2,3]):
>
> {% set outer_scope_variable = 0 %}
>  {% for item in items %}
> <p>inside for: {{ outer_scope_variable }}</p>
>  {% set outer_scope_variable = outer_scope_variable + 1 %}
> {% endfor %}
> <p>outside for: {{ outer_scope_variable }}</p>
>
>
> This loops over the for-loop three times (because the list is 3 items
> long) and increments the variable each time. I expected that using the
> variable outside the for-loop, after the for-loop, would return the
> incremented value (2). Instead it returns the original value (0).
>
> Thanks!
>

Re: [flask] Is there a way to retrieve the value of a variable set within a for loop?

From:
Ben Michie
Date:
2012-07-16 @ 11:18
Ahh yes, I did see that stackoverflow thread. My example was a bit too
simplistic actually. I intend to increment the variable multiple times for
each pass of the for loop. I'm not sure how many times it will increment
until runtime. Using the stackoverflow example I came up with this:

{% set exists = [0] %}
{% for i in range(5) %}
{% do exists.insert(0,exists[0]+1) %}
{% do exists.pop() %}
{% endfor %}
{{ exists }}

Which works. What do you think?

On 16 July 2012 20:33, Redouane Zait <unrealdz@gmail.com> wrote:

> you don't even need that, if the value you're looking for is the number of
> the items in list (number of iterations) you could try {{ items|length }}
> or else, check this out
> 
http://stackoverflow.com/questions/4870346/can-a-jinja-variables-scope-extend-beyond-in-an-inner-block
>
> On Mon, Jul 16, 2012 at 10:40 AM, Ben Michie <ben@tinku.net> wrote:
>
>> Hello! Is there a way to retrieve the value of a variable set within a
>> for loop? The way I thought it would work is as follows (items is a passed
>> in list [1,2,3]):
>>
>> {% set outer_scope_variable = 0 %}
>>  {% for item in items %}
>> <p>inside for: {{ outer_scope_variable }}</p>
>>  {% set outer_scope_variable = outer_scope_variable + 1 %}
>> {% endfor %}
>> <p>outside for: {{ outer_scope_variable }}</p>
>>
>>
>> This loops over the for-loop three times (because the list is 3 items
>> long) and increments the variable each time. I expected that using the
>> variable outside the for-loop, after the for-loop, would return the
>> incremented value (2). Instead it returns the original value (0).
>>
>> Thanks!
>>
>
>

Re: [flask] Is there a way to retrieve the value of a variable set within a for loop?

From:
Joe Esposito
Date:
2012-07-16 @ 11:26
This seems like the kind of logic you should calculate in your view,
passing the result to render_template. That, or create a filter
like Redouane is suggesting. Is there any reason to put it in the template?

On Mon, Jul 16, 2012 at 7:18 AM, Ben Michie <ben@tinku.net> wrote:

> Ahh yes, I did see that stackoverflow thread. My example was a bit too
> simplistic actually. I intend to increment the variable multiple times for
> each pass of the for loop. I'm not sure how many times it will increment
> until runtime. Using the stackoverflow example I came up with this:
>
> {% set exists = [0] %}
> {% for i in range(5) %}
> {% do exists.insert(0,exists[0]+1) %}
>  {% do exists.pop() %}
> {% endfor %}
> {{ exists }}
>
> Which works. What do you think?
>
> On 16 July 2012 20:33, Redouane Zait <unrealdz@gmail.com> wrote:
>
>> you don't even need that, if the value you're looking for is the number
>> of the items in list (number of iterations) you could try {{ items|length }}
>> or else, check this out
>> 
http://stackoverflow.com/questions/4870346/can-a-jinja-variables-scope-extend-beyond-in-an-inner-block
>>
>> On Mon, Jul 16, 2012 at 10:40 AM, Ben Michie <ben@tinku.net> wrote:
>>
>>> Hello! Is there a way to retrieve the value of a variable set within a
>>> for loop? The way I thought it would work is as follows (items is a passed
>>> in list [1,2,3]):
>>>
>>> {% set outer_scope_variable = 0 %}
>>>  {% for item in items %}
>>> <p>inside for: {{ outer_scope_variable }}</p>
>>>  {% set outer_scope_variable = outer_scope_variable + 1 %}
>>> {% endfor %}
>>> <p>outside for: {{ outer_scope_variable }}</p>
>>>
>>>
>>> This loops over the for-loop three times (because the list is 3 items
>>> long) and increments the variable each time. I expected that using the
>>> variable outside the for-loop, after the for-loop, would return the
>>> incremented value (2). Instead it returns the original value (0).
>>>
>>> Thanks!
>>>
>>
>>
>

Re: [flask] Is there a way to retrieve the value of a variable set within a for loop?

From:
Ben Michie
Date:
2012-07-17 @ 09:30
The only real reason is it seemed more logical to me to have it in the
template. I am doing my counter incrementing there (I use the results of
the increment to set HTML id fields) and so it made sense to me to get the
final counter amount at the end of it (after the for loop has finished). I
was just surprised that jinja2 wouldn't give me the value and thought I
must have missed something.

I actually have two ways I can achieve this now. Predict the result of the
counter by adding up my list lengths and passing the result to the
template, or I can use my pop/insert method. Or a filter, if I feel like
adding a third method :-P

Thank you both for your help! :-)

On 16 July 2012 21:26, Joe Esposito <espo58@gmail.com> wrote:

> This seems like the kind of logic you should calculate in your view,
> passing the result to render_template. That, or create a filter
> like Redouane is suggesting. Is there any reason to put it in the template?
>
>
> On Mon, Jul 16, 2012 at 7:18 AM, Ben Michie <ben@tinku.net> wrote:
>
>> Ahh yes, I did see that stackoverflow thread. My example was a bit too
>> simplistic actually. I intend to increment the variable multiple times for
>> each pass of the for loop. I'm not sure how many times it will increment
>> until runtime. Using the stackoverflow example I came up with this:
>>
>> {% set exists = [0] %}
>> {% for i in range(5) %}
>> {% do exists.insert(0,exists[0]+1) %}
>>  {% do exists.pop() %}
>> {% endfor %}
>> {{ exists }}
>>
>> Which works. What do you think?
>>
>> On 16 July 2012 20:33, Redouane Zait <unrealdz@gmail.com> wrote:
>>
>>> you don't even need that, if the value you're looking for is the number
>>> of the items in list (number of iterations) you could try {{ items|length }}
>>> or else, check this out
>>> 
http://stackoverflow.com/questions/4870346/can-a-jinja-variables-scope-extend-beyond-in-an-inner-block
>>>
>>> On Mon, Jul 16, 2012 at 10:40 AM, Ben Michie <ben@tinku.net> wrote:
>>>
>>>> Hello! Is there a way to retrieve the value of a variable set within a
>>>> for loop? The way I thought it would work is as follows (items is a passed
>>>> in list [1,2,3]):
>>>>
>>>> {% set outer_scope_variable = 0 %}
>>>>  {% for item in items %}
>>>> <p>inside for: {{ outer_scope_variable }}</p>
>>>>  {% set outer_scope_variable = outer_scope_variable + 1 %}
>>>> {% endfor %}
>>>> <p>outside for: {{ outer_scope_variable }}</p>
>>>>
>>>>
>>>> This loops over the for-loop three times (because the list is 3 items
>>>> long) and increments the variable each time. I expected that using the
>>>> variable outside the for-loop, after the for-loop, would return the
>>>> incremented value (2). Instead it returns the original value (0).
>>>>
>>>> Thanks!
>>>>
>>>
>>>
>>
>