jdeibele
jdeibele

Reputation: 35

Django: want to use loop.counter to assign letter for Google Maps marker

I've got a variable number of items, somewhere between 0 and 20.

I'd like to list these with Google Static Maps, showing a little "a" for the first one, a "b" for the second one and so on.

I'm a newbie using Google App Engine so I'm constrained to 0.96 (unless I use various patches, which I don't want to do. Because I'm a newbie.)

&markers={% for item in results %}{{item.latitude}},{{item.longitude}}{% if not forloop.last %}|{% endif %}{% endfor %}

is working fine to provide a list of red markers.

&markers={% for item in results %}{{item.latitude}},{{item.longitude}},{{forloop.counter0}}{% if not forloop.last %}|{% endif %}{% endfor %}

gets me 0-9 on the map.

For now, I've cut the result set down to 10. I'd like to go back to 20. Is there a way of using the loopcounter and slice (as in {{ alpha_list|slice:":loop_counter"}} ? I struggled with various incantations, trying {{ }} around loop_counter and without and couldn't get it to work.

Thanks!

Upvotes: 1

Views: 924

Answers (3)

Dave W. Smith
Dave W. Smith

Reputation: 24966

If you want do it entirely within the template, you can use the cycle tag.

Something like the following, with ... expanded:

{% cycle 'a' 'b' ... 'z' as alphabet %}
&markers={% for item in results %}{{item.latitude}},{{item.longitude}},{% cycle alphabet %}{% if not forloop.last %}|{% endif %}{% endfor %}

Upvotes: 1

phillc
phillc

Reputation: 7461

create a template tag using method provided here

How do I iterate through a string in Python?

Upvotes: 0

tghw
tghw

Reputation: 25303

Easiest would be to write a template tag. There's a good tut, but the code would basically be:

def inttoalpha(n):
    a = ord('A')
    return chr(a+n)

Upvotes: 2

Related Questions