{% set views_accordion_items = [] %}

{% set i = 0 %}
{% set len = rows|length %}
{% for row in rows %}
  {% set i = i + 1 %}

  {% set accordion_item_header = row.title %}
  {% set accordion_item_body = row.content %}

  {% set accordion_item_expanded = (
      (i == 1 and options.collapse.first is defined and options.collapse.first > 0)
      or (i == len and options.collapse.last is defined and options.collapse.last > 0)
      or (i != 1 and i != len and options.collapse.middle is defined and options.collapse.middle > 0)
      ) ? true : false
  %}

  {% set accordion_item = {'header': accordion_item_header, 'body': accordion_item_body, 'expanded': accordion_item_expanded } %}
  {% set views_accordion_items = views_accordion_items|merge([accordion_item]) %}

{% endfor %}

{%
  include 'vartheme_bs5:accordion' with {
    accordion_id: id,
    items: views_accordion_items,
    header_tag: 'h4',
    flush: false,
    accordion_utility_classes: classes,
    toggle_all: true,
  }
%}
