Snippet List
This tag is equivalent to {% cycle %} but resets when we exit the
containing loop.
See Django ticket "Cycle tag should reset after it steps out of scope"
[https://code.djangoproject.com/ticket/5908](https://code.djangoproject.com/ticket/5908)
This code is a lightly modified version of Simon Litchfield's
attachment on that ticket.
This is a customized version of the default `for` template tag which takes an optional `{% else %}` clause that will be displayed if the given array is empty.
from django.template import *
>>> t1 = Template("""
{% load mytags %}
{% for athlete in athlete_list %}
{{ athlete }}
{% else %}
No athlete in list!
{% endfor %}
""")
>>> c1 = Context(
{'athlete_list':
['me', 'myself', 'I']
})
>>> t1.render(c1)
u'me myself I '
>>> c2 = Context({})
>>> t1.render(c2)
u'No athlete in list!'
If you want to automatically override the builtin `for` template-tag add it to the builtins:
from django.template import add_to_builtins
add_to_builtins('python.path.to.mytags')
- template
- tags
- template-tag
- templatetags
- forloop
2 snippets posted so far.