djula icon indicating copy to clipboard operation
djula copied to clipboard

Provide an empty tag for use with for loops

Open nmunro opened this issue 1 year ago • 2 comments

Provide an empty tag for use with for loops, this would be useful if a loop failed to start.

For example

{% for x in y %}
    <p>{{ x }}</p>
{% empty %}
    <p>Nothing to show</p>
{% endfor %}

nmunro avatar Dec 30 '24 21:12 nmunro

Hello, strange to me but interesting. Does this exist in Django? (I don't thing so) It can be done with a if tag of course, thus adding a level of nesting.

vindarel avatar Dec 31 '24 10:12 vindarel

It does exist in django https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#for-empty

I did think of simply nesting it using an if, but this is how django does it, however this also comes from the fact that python has optional else blocks that can be attached to loops.

nmunro avatar Dec 31 '24 10:12 nmunro