Lets say I have an object
foo =[ob1, obj2, obj3 ... obj100 ...]
Now I would like to loop through the开发者_如何学运维m in my template but in groups of 5,
So there will be 5 objects per "row". and in each row I would like to add a class to the first and last one in that row.
Is there a clean why of doing this?
You can use cycle tag:
<td class="{% cycle 'first' '' '' '' 'last' %}">
</td>
or:
<td{% cycle ' class="first"' '' '' '' ' class="last"' %}">
</td>
There's already an even and uneven adding template filter in django itself. Look at it and create your own custom filter..
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
register = template.Library()
@register.filter
def count_each_5th_object(value, arg):
counter = 0
for object in value:
counter += 1
if counter % arg etcetera...
But if you are trying to achieve this result purely for styling reasons I recommend using css only... look into: first-child, nth-child(5n+5) and last-child
精彩评论