0

I have created a custom views-view-field template and added some custom html and views field similar to this

<div class="{{ fields.my_text_field.content }}">
  {{ fields.my_media_field.content }}
</div>

The trouble I am having is that I want to add classes on certain rows only. Similar to below

<div class="{{ fields.my_text_field.content }} not-row-2">
  {{ fields.my_media_field.content }}
</div>
<div class="{{ fields.my_text_field.content }} is-row-2">
  {{ fields.my_media_field.content }}
</div>
<div class="{{ fields.my_text_field.content }} not-row-2">
  {{ fields.my_media_field.content }}
</div>

In the views-view template I can run a loop and check the count and add a class using code similar to below, but then this class is in a different html wrapper. Not in the same wrapper as {{ fields.my_text_field.content }}.

{% if (loop.index == 2 ) %}
  {% set rowclass = 'is-row-2' %}
{% else %}
  {% set rowclass = 'not-row-2' %}
{% endif %}
  <div class="{{rowclass}">
  {{ row.content }}
  </div>
{% endfor %} 

How can I add a class to the child (row.content) based on the row index of the parent at views-view-field. Thanks

1
  • Your question is not clear to me. Hopefully I am asking you the right question that are your trying to add a class to the wrapper of specific field in the views (e.g my_media_field)? Dec 15 '21 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.