Score:1

How to separate Twig array via batch

mw flag

I'm trying to structure the output of a view into specific HTML for a carousel/slider I'm generating, basically I'm trying to turn a regular list of X people into groups, which I'm then dividing further for the purposes of structuring the HTML as I need

currently my rows variable contains X items (likely between 50-100)

  • I am trying to split that into pages of 12 items
  • Each page has 4 rows and 3 columns

So I want the HTML to look like this

<div class="page">
  <div class="row">
    <div class="col">A</div>
    <div class="col">A</div>
    <div class="col">A</div>
  </div>
  <div class="row">
    <div class="col">A</div>
    <div class="col">A</div>
    <div class="col">A</div>
  </div>
...
</div>
...

And my current twig is setup like this

{% set blank = [] %}

<div id="people-carousel-wrapper">
  <div id="people-carousel-container">
    {% for page in rows|batch(12) %}
      <div class="people-page">
        <div class="container">
          {% for row in page|batch(4, blank) %}
            <div class="row">
              {% for people in row|batch(3, blank) %}
                <div class="people-column col">
                  {{- people.content -}}
                </div>
              {% endfor %}
            </div>
          {% endfor %}
        </div>
      </div>
    {% endfor %}
  </div>
</div>

What I'd like to do is have it render out the full list of 12 items per page, regardless of if the number of items divides nicely by 12, except I don't seem to be able to make it render out the remaining HTML items, the twig batch page seems to suggest that you just need to fill in the second parameter for the batch function and it will use that item if nothing is there, at the moment it just stops when there are no more items

Score:1
mw flag

Figured it out for anyone wanting to find a similar sort of resolution

I needed to put the 'fill in the blank' part on my first batch, I realised that if I'm splitting that into groups of 12, as long as I fill in the blank spaces there, then I'll know that I've always got 12 items to group into the rows/cols as I need

{% set blank = [] %}

<div id="people-carousel-wrapper">
  <div id="people-carousel-container">
    {% for page in rows|batch(12, blank) %}
      <div class="people-page">
        <div class="container">
          {% for row in page|batch(3) %}
            <div class="row">
              {% for people in row %}
                <div class="people-column col">
                  {{- people.content -}}
                </div>
              {% endfor %}
            </div>
          {% endfor %}
        </div>
      </div>
    {% endfor %}
  </div>
</div>
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.