new feature idea: even and odd forloop iteration variables

232 views
Skip to first unread message

skazhy

unread,
Sep 19, 2011, 6:58:51 AM9/19/11
to Django developers
Hi all!

I've been working with Django for a while now, but today I decided to
make a first contribution to Django that would make templates less
ugly by adding these two values to loop_dict (in django/template/
defaulttags.py):

loop_dict['even'] = (i %2 == 0)
loop_dict['odd'] = (i %2 != 0)

Using {% if forloop.even %} would be more clear and readable than {%
if forloop|divisibleby:"2" %}.
As I am new to working *on* Django I don't know the history of this
idea (if there is one) and the correct way of sending patches, all
comments (and corrections!) are appreciated!


-Karlis Lauva /skazhy/

Jonas H.

unread,
Sep 19, 2011, 8:08:15 AM9/19/11
to django-d...@googlegroups.com

Travis Swicegood

unread,
Sep 19, 2011, 2:57:36 PM9/19/11
to django-d...@googlegroups.com
Hey Karlis;

This is a cool idea.  I don't do enough template work to always remember the cycle syntax, but an odd/even is pretty simple to remember.

As for getting this into Django, check out the Contributing Docs[1].  It has everything you need on how to help out.  In particular the Requesting New Features[2] section will be of interest to you.  The first thing to do is bring it up here on the list (like you've done) and see if others are interested in adding it.  You might also want to include links to any code that you've changed so other's can take a look at the code.

Hope this help,
-T


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




--
Travis Swicegood | @tswicegood (most everywhere) | Senior Open Source Engineer @ Texas Tribune / Armstrong

Russell Keith-Magee

unread,
Sep 22, 2011, 9:24:05 AM9/22/11
to django-d...@googlegroups.com
On Tue, Sep 20, 2011 at 2:57 AM, Travis Swicegood <tra...@domain51.com> wrote:
> Hey Karlis;
> This is a cool idea.  I don't do enough template work to always remember the
> cycle syntax, but an odd/even is pretty simple to remember.

I disagree, for a number of reasons

* There should be Only One Way To Do It

* Cycle syntax isn't that complex

* Cycle syntax is a lot more flexible -- it allows, for example,
cycles by 3 instead of just even/odd, or a/a/b cycles, or whatever
other crazy things you might need.

* I already have difficulty remembering the names of the context
variables that the for loop adds.

So, call me a -0 on a specific even/odd counter on the for loop.

Yours,
Russ Magee %-)

Luke Plant

unread,
Sep 24, 2011, 11:37:50 AM9/24/11
to django-d...@googlegroups.com
Hi skazhy,

I agree with Russell - this overlaps completely with 'cycle' or
'divisibleby', and we prefer Only One Way To Do It.

Thanks for the idea though,

Luke

--
I never hated a man enough to give him his diamonds back. (Zsa Zsa
Gabor)

Luke Plant || http://lukeplant.me.uk/

Reply all
Reply to author
Forward
0 new messages