开发者

Refactor Django template forloop

I feel like there has to be a cleaner way to do somthing like t开发者_JS百科his. I have 15 or so objects that list out with three on a row. Anyone know a better solutions.

<ul>
    {% for object in object_list %}

        <li
            {% ifequal forloop.counter 1  %}class="first"{% endifequal %}
            {% ifequal forloop.counter 4  %}class="first"{% endifequal %}
            {% ifequal forloop.counter 7  %}class="first"{% endifequal %}
            {% ifequal forloop.counter 10  %}class="first"{% endifequal %}
            {% ifequal forloop.counter 13  %}class="first"{% endifequal %}   
        >
            {{ object.title }}
        </li>

        {% ifequal forloop.counter 3 %}<div class="clear"></div>{% endifequal %}
        {% ifequal forloop.counter 6 %}<div class="clear"></div>{% endifequal %}
        {% ifequal forloop.counter 9 %}<div class="clear"></div>{% endifequal %}
        {% ifequal forloop.counter 12 %}<div class="clear"></div>{% endifequal %}
        {% ifequal forloop.counter 15 %}<div class="clear"></div>{% endifequal %}
    {% endfor %}
</ul>

For the second loop you can do

{% if forloop.counter|divisibleby:"3" %}<div class="clear"></div>{% endif %}

But 1,4,7,10,13 dont have a common denominator.

Any help is appreciated.


You're looking for forloop.counter0.

{% if forloop.counter0|divisibleby:"3" %}<div class="clear"></div>{% endif %}


You could use slicing to split them into your groups of 3 rows, and then do the appropriate operation on each 3 unit slice.

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜