djangosnippets.org: Latest snippets tagged with 'loop'https://djangosnippets.org/tags/loop/2011-09-09T16:53:59.170999-05:00Duplicating Template Tag
2011-09-09T16:53:59.170999-05:00skarphacehttps://djangosnippets.org/snippets/2545/<p>This template tag will duplicate its contents according to a variable or integer supplied to it.</p>
<pre><code>{% duplicate 3 %}a{% endduplicate %}
</code></pre>
<p>This would return:</p>
<blockquote>
<p>aaa</p>
</blockquote>
Freely redistributableWhile loop template tag
2010-07-05T16:00:27.840870-05:00gsakkishttps://djangosnippets.org/snippets/2096/<p>The missing <code>while</code> template tag. Built on top of http://djangosnippets.org/snippets/2093/, it also supports <code>break</code> and <code>continue</code> out of the box.</p>
Freely redistributableContinuing and breaking from loops in Django templates
2010-07-04T14:28:44.717080-05:00gsakkishttps://djangosnippets.org/snippets/2093/<p>This snippet makes Django templates support <code>break</code> and <code>continue</code> 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.</p>
<p><code>break</code> and <code>continue</code> are implemented as template filters, with the input value being the loop …</p>
Freely redistributableSelfForeignKey to prevent hierarchical loops
2010-02-01T15:56:29.888726-06:00jamesgpearcehttps://djangosnippets.org/snippets/1906/<p>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:</p>
<h1>Data modelling Back to the Future</h1>
<blockquote>
<p>grandfather > father > son > father > ...</p>
</blockquote>
<p>Using this field instead of the …</p>
Freely redistributableTemplate range loop
2010-01-28T14:24:16.924850-06:00nfghttps://djangosnippets.org/snippets/1899/<p>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.</p>
<pre><code>Syntax:
{% range end as i %}
{{ i }} …</code></pre>
Freely redistributablecounter templatetag
2009-11-12T09:42:00.508900-06:00bcurtuhttps://djangosnippets.org/snippets/1794/<p>The counter initializes the variable to 0, and next it increments one by one:</p>
<pre><code>{% load counter_tag %}
{% for pet in pets %}
{% if pet.is_cat %}
{% counter cats %}
{% else %}
{% counter dogs %}
{% endif %}
{% endfor %}
# cats: {{cats}}
# dogs: …</code></pre>
Freely redistributable