On 2008-03-24, at 1312, Tim Sawyer wrote:
>
> Hi Folks,
>
> I want to do this:
>
> <table>
>  <tr>
>    {% for image in Images %}
>      <td><a href="/gallery/image/{{image.id}}/"><img src="{% thumbnail
> image.image 200x100 %}"/></a><br/>
>        {{ image.name }}<br/>
>        {{ image.comment }}<br/>
>      </td>
>      {% if forloop.counter % 4 %}
>        </tr>
>        <tr>
>      {% endif %}
>    {% endfor %}
>  </tr>
> </table>
>
> which should hopefully give me a gallery of images where each line  
> has four
> thumbnails on it.
>
> Unfortunately, I can't do the mod (%) in the if statement, as it's  
> not valid
> syntax.  Is there a way around this, or a simpler solution?



What about just converting it to a 2-dimensional array in the view?  
For example, using grouper() from the itertools recipes [1]:

def grouper(n, iterable, padvalue=None):
     "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'),  
('g','x','x')"
     return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

Images = grouper(4, Images)


Then, the logic in the template is straightforward:

<table>
{% for row in Images %}
     <tr>
     {% for image in row %}
         <td>{% if image %} ... image detail ... {% endif %}</td>
     {% endfor %}
     </tr>
{% endfor %}
</table>


scott.


[1]: http://docs.python.org/lib/itertools-recipes.html

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to