Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 18, 2011 13:58:44

G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

template tag arithmetic


Hi everyone,
Is there a way to do simple loop counter manipulation within the
template? I.e something along the lines of:

{% for x in a %}
loop number = {{forloop.counter +1}}
{% endfor %}

with the intention of

2
3
4
....

as output. I realise the above doesn't work, but is there any scope
for this sort of thing within the templating language itself?

Regards,
G

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 18, 2011 14:02:37

T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

template tag arithmetic


Am 18.01.2011 um 14:53 schrieb GD:

>
> Hi everyone,
> Is there a way to do simple loop counter manipulation within the
> template? I.e something along the lines of:
>
> {% for x in a %}
> loop number = {{forloop.counter +1}}
> {% endfor %}
>
> with the intention of
>
> 2
> 3
> 4
> ....
>
> as output. I realise the above doesn't work, but is there any scope
> for this sort of thing within the templating language itself?
>http://docs.djangoproject.com/en/dev/ref/templates/builtins/look for 'forloop.counter'

good luck,
TR



> Regards,
> G
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 18, 2011 14:47:05

Konrad D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

template tag arithmetic


On 18 January 2011 15:02, Thomas <tho...@googlemail.com> wrote:
>
> Am 18.01.2011 um 14:53 schrieb GD:
>
>>
>> Hi everyone,
>>     Is there a way to do simple loop counter manipulation within the
>> template? I.e something along the lines of:
>>
>> {% for x in a %}
>>      loop number = {{forloop.counter +1}}
>> {% endfor %}
>>
>> with the intention of
>>
>> 2
>> 3
>> 4
>> ....
>>
>> as output. I realise the above doesn't work, but is there any scope
>> for this sort of thing within the templating language itself?
>>


You can write a filter:http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/#writing-custom-template-filters>> {% for x in a %}
>>      loop number = {{forloop.counter|increase}}
>> {% endfor %}

cheers,
Konrad

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 18, 2011 15:24:25

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

template tag arithmetic


On 18 January 2011 15:46, Konrad Delong <kon...@gmail.com> wrote:
> On 18 January 2011 15:02, Thomas <tho...@googlemail.com> wrote:
>>
>> Am 18.01.2011 um 14:53 schrieb GD:
>>
>>>
>>> Hi everyone,
>>>     Is there a way to do simple loop counter manipulation within the
>>> template? I.e something along the lines of:
>>>
>>> {% for x in a %}
>>>      loop number = {{forloop.counter +1}}
>>> {% endfor %}
>>>
>>> with the intention of
>>>
>>> 2
>>> 3
>>> 4
>>> ....
>>>
>>> as output. I realise the above doesn't work, but is there any scope
>>> for this sort of thing within the templating language itself?
>>>
>
>
> You can write a filter:
>
>http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/#writing-custom-template-filters>
>>> {% for x in a %}
>>>      loop number = {{forloop.counter|increase}}
>>> {% endfor %}
>
> cheers,
> Konrad
>

Actually, that case is already covered by builtin filter "add":

{% for x in a %} {{ forloop_counter|add:"1" }} {% endfor %}

:)

--
Łukasz Rekucki

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 18, 2011 15:53:36

n.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

template tag arithmetic


http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#addThere is loop.counter and loop.counter0. loop.counter starts from 1,
and loop.counter0 starts from 0. The 3rd time through a for loop
loop.counter is 3 and loop.counter0 is 2.

If you want to add things try something like this:

{{ value|add:"2" }}

if value was 3 then 5 would be output.



On Jan 18, 1:53 pm, GD <gerarddev...@googlemail.com> wrote:
> Hi everyone,
>      Is there a way to do simple loop counter manipulation within the
> template? I.e something along the lines of:
>
> {% for x in a %}
>       loop number = {{forloop.counter +1}}
> {% endfor %}
>
> with the intention of
>
> 2
> 3
> 4
> ....
>
> as output. I realise the above doesn't work, but is there any scope
> for this sort of thing within the templating language itself?
>
> Regards,
> G

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support