How to get 'switch-case' statement functionality in Django templates?

Czlowiekwidmo picture Czlowiekwidmo · Apr 7, 2009 · Viewed 36.1k times · Source

I found a link to have a 'switch' tag in Django templates, but I was wondering if this can be somehow achieved without it. Using only the stuff which comes with Django? Basically is there other way then using multiple 'if' or 'ifequal' statements?

Thanks in advance for any tips/suggestions.

Answer

Steve Bennett picture Steve Bennett · May 1, 2012

As of Django 1.4, there is {% elif %}:

{% if a %}
  thing
{% elif b %}
  other thing
{% elif c %}
  another thing
{% endif %}