Snippet List
The `{% switch %}` tag compares a variable against one or more values in
`{% case %}` tags, and outputs the contents of the matching block. An
optional `{% else %}` tag sets off the default output if no matches
could be found:
{% switch result_count %}
{% case 0 %}
There are no search results.
{% case 1 %}
There is one search result.
{% else %}
Jackpot! Your search found {{ result_count }} results.
{% endswitch %}
Each `{% case %}` tag can take multiple values to compare the variable
against:
{% switch username %}
{% case "Jim" "Bob" "Joe" %}
Me old mate {{ username }}! How ya doin?
{% else %}
Hello {{ username }}
{% endswitch %}
This tag allow you to use C-like switch tag in your templates.
It is useful for sequencial and repetitive `{% ifequal %}` tags.
To install it in your project, you just need to follow [these instructions](http://www.djangoproject.com/documentation/templates_python/#extending-the-template-system)
- tag
- conditional
- switch
- case
Meant mostly as a demo of how to do complex block tags, here's a switch/case implementation.
It's deliberately simplistic: no default cases, no multiple values for the same case, etc. This is so that you can focus on the technique.
Pay particular attention to how both switch and case pull out their child nodes and save them for later rendering. This is a very useful technique for these types of "structured" template tags.
- templatetag
- switch
- case
- flow
- logic-doesnt-belong-in-templates
3 snippets posted so far.