Duplicating Template Tag
This template tag will duplicate its contents according to a variable or integer supplied to it. {% duplicate 3 %}a{% endduplicate %} This would return: > aaa
- template-tag
- duplicate
- loop
This template tag will duplicate its contents according to a variable or integer supplied to it. {% duplicate 3 %}a{% endduplicate %} This would return: > aaa
The missing `while` template tag. Built on top of http://djangosnippets.org/snippets/2093/, it also supports `break` and `continue` out of the box.
This snippet makes Django templates support `break` and `continue` in loops. It is actually more powerful than the respective Python statements as it allows breaking and continuing from an outer loop, not just the innermost. `break` and `continue` are implemented as template filters, with the input value being the loop variable. For example, to break from the current `for` loop use `forloop|break`, and to continue from the next outer loop use `forloop.parentloop|continue`. The implementation monkeypatches Django (specifically Nodelist and ForNode) and has been tested on v1.2 with Python 2.6.
When you have a model containing a field that is a foreign key back to the same model, you could find yourself with a hierarchy with an infinite loop: #Data modelling Back to the Future > grandfather > father > son > father > ... Using this field instead of the standard ForeignKey will ensure that no model instance is saved that has itself as an ancestor, breaking the relationship if it does. (Enhancements: I am sure one would want to better enhance this with appropriate error handling instead of silently disconnecting the relationship. And the relevant forms ought not show ancestors in the field's widget to reduce the chances of this happening in the first place.)
This tag is meant to mimic the python use of range in a for-loop: 'for i in range(start, end, step)'. It is implemented like a loop and it takes both variable names from the context and constant integers as arguments. Syntax: {% range end as i %} {{ i }} {% endrange %} {% range start:end as i %} {{ i }} {% endrange %} {% range start:step:end as i %} {{ i }} {% endrange %}
The counter initializes the variable to 0, and next it increments one by one: {% load counter_tag %} {% for pet in pets %} {% if pet.is_cat %} {% counter cats %} {% else %} {% counter dogs %} {% endif %} {% endfor %} # cats: {{cats}} # dogs: {{dogs}}
6 snippets posted so far.