thanks Bill for the detailed answer..it really helped me to understand
the logic
harry
Bill Freeman wrote:
> Looking at the code in django/template/defaulttags.py, where len_values
> is the number of itterations in the loop:
>
> counter0 runs from 0 through len_values-1
> counter runs from 1 through len_values
> revcounter runs from len_values down to 1 (essentially it is
> len_values - counter0)
> revcounter0 runs from len_values-1 down to 0 (len_values-counter)
>
> Since you're printing 'and' after you print tag.name, you want to do
> it in the penultimate
> iteration. At this point revcounter will be 2. That is, counting
> this iteration, there are
> 2 iterations left in the loop. revcounter9 will be 1 at this point,
> because there is one
> iteration after the current one.
>
> So you can check {% ifequal forloop.revcounter 2 %} or you can check
> {% ifequal forloop.revcounter0 1 %}.
>
> By the way, you don't need the forloop last check, as either of the above
> automatically don't happen on the last iteration.
>
> An alternative would be to (conditionally) print the "and" before
> tag.name in the loop,
> where you only have to use last:
>
> ...
> {% for tag in taglist %%}{% if forloop.last %} and{% endif %} {{
> tag.name }}{% endfor %}
>
> And note that {% if forloop.last %} is roughly equivalent to {%
> ifequal forloop.revcounter0 0 %} or
> {% ifequal forloop.revcounter 1 %}.
>
> To get a better handle on what has which value when, you could sprinkle
> some {{ forloop.revcounter }} and {{ forloop.revcounter0 }} instances in your
> loop.
>
> Bill
>
> On Tue, Feb 2, 2010 at 4:30 AM, harryos <[email protected]> wrote:
> > hi
> > While trying out lessons on templates.I tried using the variables in
> > 'for' to print the names of 2 tags in a list called taglist.(template
> > code is given at the bottom)
> > I could n't understand why ifequal forloop.revcounter0 1 is used
> > instead of revcounter..
> > I tried to follow the logic using revcounter variable,
> >
> > 1st iteration of for:
> > print tagname of first tag
> > current iteration is not the last
> > check if 1 iteration is left ,revcounter should return 1 since 1
> > more iter is left,so should print the word 'and'
> > 2nd iteration:
> > print tagname of second tag
> > forloop.last is true,nothing printed
> > forloop ends
> >
> > According to above logic I should be getting the result as
> > firsttag and secondtag
> > Still ,I got the correct result only when i used ifequal
> > forloop.revcounter0 1
> >
> > Can someone tell me how I got the logic wrong..I am quite confused
> > thanks
> > harry
> >
> > {% load tagging_tags %}
> > {% tags_for_object object as taglist %}
> >
> > {% if taglist.count %}
> > {{ taglist.count}} Tags for this link are :
> > {% for tag in taglist %}
> > {{tag.name}}
> > {% if forloop.last %} {% else %}
> > {% ifequal forloop.revcounter 1 %} and {% else %},{% endifequal %}
> > {% endif %}
> > {% endfor %}
> > {% else %}
> > No tags for this link
> > {% endif %}
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.