title: Django, templates, for loops, and cycles
body: <p>(tl;dr at the bottom)</p>
  
  <p>Let me try to explain what I'm trying to accomplish: I have a two dimensional array, and I would like to display its contents a certain way. I want "rows", and each row can display no more than three "objects", for lack of a better word. So I want to iterate over the array and create my HTML in the process. My idea is this: every "first of three" elements in the array should open up the "row". Every "third of three" elements should close the "row". However, if the last element in the [inner] array does not happen to be the "third of three", it should still close the row. So, for example, if we had something like <code>L=[ [0,1,2,3,4], [5,6,7] ]</code>, I would want to display it like so:</p>
  
  <pre><code>0  1  2
  3  4
  
  5  6  7
  </code></pre>
  
  <p>Which might be marked up like:</p>
  
  <pre><code>&lt;div&gt;0 1 2&lt;/div&gt;
  &lt;div&gt;3 4&lt;/div&gt;
  &lt;div&gt;5 6 7&lt;/div&gt;
  </code></pre>
  
  <p>My first thought was to simply use the modulo operator and see if each iteration was the "first", "second", or "third" of a row, but Django templates don't directly support modulo (more on that later). </p>
  
  <p>So I came up with template code like this:</p>
  
  <pre><code>{% for x in L %}
   {% for y in x %}
    {% cycle '&lt;div&gt;' '' '' %}
     {{ y }}
    {% cycle '' '' '&lt;/div&gt;' %}
   {% endfor %}&lt;br/&gt;
  {% endfor %}
  </code></pre>
  
  <p>And this was a good start. Taking the above example, on <code>0</code> we would open a div, on <code>1</code> do nothing, on <code>2</code> close the div, on <code>3</code> open it, and on <code>4</code>... well, it wouldn't close, because <code>4</code> was not the "third in a series of 3". So, while Django templates don't have modulo, they do have a divisibleby check, so I came up with additional logic to say, if we hit the last element of the [inner] for loop, and it also is not divisible by 3 (so we don't have a duplicate close), then close the div:</p>
  
  <pre><code>{% for x in z %}
   {% for y in x %}
    {% cycle '&lt;div&gt;' '' '' %}
     {{ y }}
    {% cycle '' '' '&lt;/div&gt;' %}
  
    {% if forloop.last %}
    {% if forloop.counter|divisibleby:"3" %}
      &lt;!-- Row Already Closed --&gt;
    {% else %}
      &lt;/div&gt;
    {% endif %}
    {% endif %}
  
   {% endfor %}&lt;br/&gt;
  {% endfor %}
  </code></pre>
  
  <p>This worked better! Now, I got through the whole first inner array with proper markup. My issue is this: apparently, Django's cycle functionality does not reset when you go out of scope of the inner for loop. What this means is, my number <code>5</code> from the above example is not opening a div like it should, it is not being recognized as the first in a cycle. In fact, it's actually being recognized as a third, and so it is closing a div! </p>
  
  <p>So I'm not sure where to go from here. This appears to be a <a href="https://code.djangoproject.com/ticket/5908">known and unfixed issues in Django</a>. Can anyone assist, please? </p>
  
  <p><strong>tl;dr</strong> I want to take a 2d array, e.g. <code>L=[ [0,1,2,3,4], [5,6,7] ]</code> and properly mark it up grouping no more than 3 at a time and without grouping any elements from multiple arrays together, like so:</p>
  
  <pre><code>0  1  2
  3  4
  
  5  6  7
  </code></pre>
  
  <p>What would be the template code for that and why? </p>