-2

I have views with 6 results. However they are displayed in 2 different ways. 2 first in one way, 2 next in second way, next in first way and last in second way. I know it is quite confusing. I have found this solution: How to handle field with multiple items but I tried this without success. My output from this field is something like:

<a href="/dignissimos-velit" hreflang="en">Dignissimos velit</a>

I want to achieve something like that:


{% set items = output %}

{% for item in items %}
  {% if loop.index <=2 %}
<h3 class="heading3 mb-xs">
    {{ item }}
</h3>
  {% endif %}
{% endfor %}

{% for item in items %}
  {% if loop.index >= 3 and loop.index <= 4 %}
<h4 class="heading4 mb-xs">
    {{ item }}
</h4>
  {% endif %}
{% endfor %}

{% for item in items %}
  {% if loop.index >= 5 and loop.index <= 6 %}
<h3 class="heading3 mb-xs">
    {{ item }}
</h3>
  {% endif %}
{% endfor %}

Maybe someone could help me with this? I would be very grateful. How iterate through output field?

2
  • What is your question?
    – Hudri
    Aug 18, 2022 at 9:29
  • How iterate through output field?
    – arek_s
    Aug 18, 2022 at 9:36

1 Answer 1

0

Ok, I found answer.

In the file views-view-fields.html.twig:

{% if row.index <= 1 %}
{{ fields.field_news_image.content }}
<div class="card__teaser">
  {{ fields.title.content }}
  {{ fields.field_news_category.content }}
  {{ fields.created.content }}
</div>
{% endif %}

{% if row.index >= 2 and row.index <= 3 %}
{{ fields.field_news_image.content }}
<div class="smallnews__teaser">
  {{ fields.title.content }}
  {{ fields.field_news_category.content }}
  {{ fields.created.content }}
</div>
{% endif %}

and in the file (one of the files) views-view-field.html.twig:

{% if row.index <= 1 %}
<h3 class="heading3 mb-xs">
{{ output -}}
</h3>
{% endif %}

{% if row.index >= 2 and row.index <=3 %}
<h4 class="heading4 mb-xs">
{{ output -}}
</h4>
{% endif %}

Overall, row.index was the solution.

Not the answer you're looking for? Browse other questions tagged or ask your own question.