1

I currently have an entries field which outputs a list of related entries on page. I'm trying to find a way to display two values for these entries.

The first would be a zero index. I'm trying to assign each entry to link up with a slider. Long story short, each of the 4 assigned entries would be listed as such

0 Entry One

1 Entry Two

2 Entry Three

3 Entry Four

The second would be to display a simple count of sorts before each entry. This would be displayed as

  1. Entry One
  2. Entry Two
  3. Entry Three
  4. Entry Four

I'm sure this must be possible but I'm unsure about the zero based index format. If anyone could help, I'd really appreciate it.

My code is currently set up as such

{% for project in sliderWork %}
   <div class="progress__wrap">
   <span data-slick-index="This is where the zero based index would be displayed" class="progressBar"></span>
     <p><i class="number">This is where the simple numbering would be displayed</i>{{project.title}</p>
   </div>
 {% endfor %}

2 Answers 2

2

There's an easier way... Twig has a built-in variable in for-loops called loop, which has loop.index and loop.index0 properties which output exactly what you're looking for:

https://twig.symfony.com/doc/3.x/tags/for.html#the-loop-variable

{% for project in sliderWork %}
   <div class="progress__wrap">
   <span data-slick-index="{{ loop.index0 }}" class="progressBar"></span>
     <p><i class="number">{{ loop.index }}</i>{{project.title}</p>
   </div>
 {% endfor %}
3
  • That's great. Thanks James, that's much easier
    – Adam
    Sep 2, 2020 at 21:56
  • @Adam any chance you could mark this as the accepted answer? Thanks!
    – Jalen Davenport
    Sep 4, 2020 at 4:49
  • No problem. Done
    – Adam
    Sep 4, 2020 at 13:01
1

I have managed to figure it out. I will use the following and it seems to work fine

{% set indexCounter = ( counter | default(0) ) %}

{% set counter = ( counter | default(0) ) + 1 %}

Hope this can be of help to someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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