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 %}
<!--Right comment-->
{% else %}
<!--Left comment-->
{% 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 %}
<div class="comment is-{% i is odd ? 'odd' : 'even' %}">
<!-- Comment content -->
</div>
{% else %}
<p class="error">No comments to display</p>
{% 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 :)