A
A
ashfedor2020-02-16 16:37:36
Twig
ashfedor, 2020-02-16 16:37:36

How to enable counter in loop in Twig on Octobercms?

Good afternoon!
I work with the Twig template engine for the first time
. For layout, you need to start the counter in the middle of the loop so that every second element enters its layout.
But the counter does not want to count

{% set posts = blogPosts.posts %}
{% set count = 1  %}
{% for post in posts %}
{% if (count % 2 > 0)%}
<div class="row no-gutters">
  <div class="col-sm-6 content__textblock">
    <h3>{{ post.title }}</h3>

    <p>{{ post.excerpt }}</p>

    <a href="{{ post.url }}"> Читать далее</a>
  </div>

  <a class="col-sm-6 hover-zoom content__imgblock" href="{{ post.url }}">

    {% for image in post.featured_images|slice(0, 1) %}
    <img class="archive__photo" src="{{ image.path }}" alt="{{ post.title }}">
    {% endfor %}

  </a>
</div>
{% else %}
<div class="row no-gutters">

  <a class="col-sm-6 hover-zoom content__imgblock" href="{{ post.url }}">

    {% for image in post.featured_images|slice(0, 1) %}
    <img class="archive__photo" src="{{ image.path }}" alt="{{ post.title }}">
    {% endfor %}

  </a>
  <div class="col-sm-6 content__textblock">
    <h3>{{ post.title }}</h3>

    <p>{{ post.excerpt }}</p>

    <a href="{{ post.url }}"> Читать далее</a>
  </div>
</div>
{% endif %}
{% count+1 %}
{% endfor %}

5e4945790a70d727044317.jpeg
I understand that such a counting design is just not suitable for a twig, but .... in general, I rested!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ashfedor, 2020-02-16
@ashfedor

Everything is very simple.
the solution is like this
{% set count = count+1 %}
just the variable needs to be re-declared

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question