<p>Instead of <code>divisableby</code> I think using Twig's built-in <a href="http://twig.sensiolabs.org/doc/tests/odd.html" rel="nofollow">odd/even test</a> might be easier in this case: </p>

<pre><code>{% for i in 0.. NbComment %}

   {% if i is odd %}

       &lt;!--Right comment--&gt;

   {% else %}

       &lt;!--Left comment--&gt;

   {% endif %}

{% endfor %}
</code></pre>

<p>And, just to suggest some minor improvements to your Twig, assuming the only difference between your comments is their alignment (left/right), you could do something like this, which would probably be a bit more DRY:</p>

<pre><code>{% for i in 0.. NbComment %}

   &lt;div class="comment is-{% i is odd ? 'odd' : 'even' %}"&gt;
      &lt;!-- Comment content --&gt;
   &lt;/div&gt;

{% else %}

   &lt;p class="error"&gt;No comments to display&lt;/p&gt;

{% endfor %}
</code></pre>

<p>With the above code, your comments will have either the class <code>.is-odd</code> or the class <code>.is-even</code> – making it easy to e.g. float comments either <code>left</code> or <code>right</code> in your CSS. There's really no need to have a full if/else conditional.</p>

<p>Also, note the <code>{% else %}</code> statement in the above <code>for</code> loop. The contained markup will output if there are no comments – which is good practice, and will save you yet another conditional :)</p>

CC BY-SA 3.0
2015-03-22T12:09:23.683Z
Thanks it worked :)
NbComment is a variable which count the number of comment i have .

    $nbComment = count($c) - 1;
Franck
CC BY-SA 3.0
2015-03-22T12:20:16.650Z
Cool – I removed most of the answer to only contain the relevant part about using odd/even. Be advised that your loop will run (and print ```<!--Right comment-->``` even if ```NbComment``` is 0 :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-23T18:41:37.327Z
Hello @Franck, would you mind marking the above answer as accepted, if you're happy with it? Closing questions as "answered" helps keeping the site tidy and informative:)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-22T11:39:13.463Z
2015-03-22T12:16:51.450Z
2015-03-22T12:16:51.450Z
Mats Mikkel Rummelhoff
<p>First sorry for my bad english.</p>

<p>What i want to do: display comments on a page, one at the right one at the left.</p>

<p>My code look like this:</p>

<pre><code>{% for i in 0.. NbComment %}
   {% if  (i is divisibleby(2)) or (i == 0) %}

       &lt;!--Right comment--&gt;

   {% else %}

       &lt;!--Left comment--&gt;

   {% endif %}
{% endfor %}
</code></pre>

<p>It works perfectly except when I only have one comment: it display nothing.
Thanks for your help.</p>

<templating><php>
Twig issue with divisibleby()
# Twig issue with divisibleby()

(Asked by: username_0 on 2015-03-22)

First sorry for my bad english.
What i want to do: display comments on a page, one at the right one at the left.
My code look like this:
{% for i in 0.. NbComment %}
   {% if  (i is divisibleby(2)) or (i == 0) %}

       

   {% else %}

       

   {% endif %}
{% endfor %}

It works perfectly except when I only have one comment: it display nothing.
Thanks for your help.

**Tags:** templating, php


---

## 1 answers

### Answer by username_1 on 2015-03-22. Score: 2

Instead of divisableby I think using Twig's built-in odd/even test might be easier in this case: 
{% for i in 0.. NbComment %}

   {% if i is odd %}

       

   {% else %}

       

   {% endif %}

{% endfor %}

And, just to suggest some minor improvements to your Twig, assuming the only difference between your comments is their alignment (left/right), you could do something like this, which would probably be a bit more DRY:
{% for i in 0.. NbComment %}

   
      
   

{% else %}

   No comments to display

{% endfor %}

With the above code, your comments will have either the class .is-odd or the class .is-even – making it easy to e.g. float comments either left or right in your CSS. There's really no need to have a full if/else conditional.
Also, note the {% else %} statement in the above for loop. The contained markup will output if there are no comments – which is good practice, and will save you yet another conditional :)

> **Comments:**
> **username_0**: Thanks it worked :) NbComment is a variable which count the number of comment i have .      $nbComment = count($c) - 1;  
> **username_1**: Cool – I removed most of the answer to only contain the relevant part about using odd/even. Be advised that your loop will run (and print ```<!--Right comment-->``` even if ```NbComment``` is 0 :)  
> **username_1**: Hello @username_0, would you mind marking the above answer as accepted, if you're happy with it? Closing questions as "answered" helps keeping the site tidy and informative:)  

---

