D
D
Daniil Vershinin2019-02-12 15:47:41
Twig
Daniil Vershinin, 2019-02-12 15:47:41

Twig - different output of even and odd posts?

5c62c01755f4e550050197.png
You need to output posts in the specified format, how to do it using Twig inside the loop: {% for post in posts %}
(how to create a variable and check its parity?)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Vershinin, 2019-02-12
@Titamik

Found what I was looking for - Twig has a special parity check construct

{% if var is even%}
  ...
{% endif %}

{% if var is odd%}
  ...
{% endif %}

V
Vladimir Proskurin, 2019-02-12
@Vlad_IT

You can use the cycle function , and use it to add the row__even and row__odd classes, and style it through css.

{% for post in posts %}
  <div class="row row__{{ cycle(['even', 'odd'], loop.index0) }}">
  </div>
{% endfor %}

If you definitely want to change the markup, then use this https://twig.symfony.com/doc/2.x/tests/divisibleby.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question