Answers.Body: <p>You have some other bug somewhere else. Check the <em>code pulling out entry details</em> to be sure that it doesn't modify <code>loop</code> or the list you are looping on, or the current item.</p>
  
  <p>This code works:</p>
  
  <pre><code>{% for item in (1..10) %}
    {% if loop.first %} [ {% endif %}
    {{ item }}
    {% if loop.index is divisible by(4) and not loop.last %}
      ] [
    {% endif %}
    {% if loop.last %} ] {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>And outputs this:</p>
  
  <pre><code>[ 1 2 3 4 ] [ 5 6 7 8 ] [ 9 10 ]
  </code></pre>
  
  <p>You can simplify it a bit:</p>
  
  <pre><code>{% for item in (1..10) %}
    {% if loop.first %} [ {% endif %}
    {{ item }}
    {% if loop.index is divisible by(4) or loop.last %}
      ] [
    {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>You also can use the <a href="https://twig.symfony.com/doc/2.x/filters/batch.html" rel="nofollow noreferrer">batch</a> filter, which I think makes things slightly clearer:</p>
  
  <pre><code>{% for b in (1..10) | batch(4) %}
    [
      {% for item in b %}
        {{ item }}
      {% endfor %}
    ]
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-11T15:46:50.083Z
Answers.Text: Indeed it was something else. A rogue line of js removing the last list item. Cheers.
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-11T14:11:53.053Z
Answers.LastActivityDate: 2017-10-11T14:11:53.053Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>I have an unusual issue with a for loop divisible by. The code works fine initially as it loops through the entries and closes a the list element on the 4th loop. However when it gets to the end of the entries and there are less than 4 left it strangely adds them into the source but does not display them visually on the page or in chromes inspector?? Has anyone come across this before? Nothing is hiding the last entries and if I change divisible by to different values it does the same only show entries that are divisible by and loses the remainder.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>{% if loop.first %}
    &lt;li&gt;
  {% endif %}
  
  ..... code pulling out entry details into divs
  
  {% if loop.index is divisible by(4) and not loop.last %}
    &lt;/li&gt;&lt;li&gt;
  {% endif %}
  {% if loop.last %}
    &lt;/li&gt;
  {% endif %}
  </code></pre>
  
Tags: <loop>
Title: divisible by strange issue
ThreadText: # divisible by strange issue
  
  (Asked by: username_0 on 2017-10-11)
  
  I have an unusual issue with a for loop divisible by. The code works fine initially as it loops through the entries and closes a the list element on the 4th loop. However when it gets to the end of the entries and there are less than 4 left it strangely adds them into the source but does not display them visually on the page or in chromes inspector?? Has anyone come across this before? Nothing is hiding the last entries and if I change divisible by to different values it does the same only show entries that are divisible by and loses the remainder.
  
  The code is as follows:
  
  ```
  {% if loop.first %}
    <li>
  {% endif %}
  
  ..... code pulling out entry details into divs
  
  {% if loop.index is divisible by(4) and not loop.last %}
    </li><li>
  {% endif %}
  {% if loop.last %}
    </li>
  {% endif %}
  
  ```
  
  **Tags:** loop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-10-11. Score: 2 | Accepted answer
  
  You have some other bug somewhere else. Check the *code pulling out entry details* to be sure that it doesn't modify `loop` or the list you are looping on, or the current item.
  
  This code works:
  
  ```
  {% for item in (1..10) %}
    {% if loop.first %} [ {% endif %}
    {{ item }}
    {% if loop.index is divisible by(4) and not loop.last %}
      ] [
    {% endif %}
    {% if loop.last %} ] {% endif %}
  {% endfor %}
  
  ```
  
  And outputs this:
  
  ```
  [ 1 2 3 4 ] [ 5 6 7 8 ] [ 9 10 ]
  
  ```
  
  You can simplify it a bit:
  
  ```
  {% for item in (1..10) %}
    {% if loop.first %} [ {% endif %}
    {{ item }}
    {% if loop.index is divisible by(4) or loop.last %}
      ] [
    {% endif %}
  {% endfor %}
  
  ```
  
  You also can use the [batch](https://twig.symfony.com/doc/2.x/filters/batch.html) filter, which I think makes things slightly clearer:
  
  ```
  {% for b in (1..10) | batch(4) %}
    [
      {% for item in b %}
        {{ item }}
      {% endfor %}
    ]
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Indeed it was something else. A rogue line of js removing the last list item. Cheers.  
  
  ---
  
  