Is it Possible to count current indexing in a loop which starts from indexing 1 as well as 0?
Twig offers special loop variables that makes it easy to know indexing of a loop. You can use loop.index to count your current indexing of the loop, which will starting at 1. If You want to start your indexing from 0 so simply add 0 after loop.index like loop.index0.
There are other loop variable as well.
Like:
- loop.revindex
- loop.revindex0
- loop.first
- loop.last
- loop.length
Aside from the documentation on Craft web site https://craftcms.com/docs/3.x/dev/twig-primer.html, Twig is documented in its own site: https://twig.symfony.com/doc/3.x/
ZealousWeb's answer is documented here: https://twig.symfony.com/doc/3.x/tags/for.html
Inside of a for loop block you can access some special variables:
- loop.index The current iteration of the loop. (1 indexed)|
- loop.index0 The current iteration of the loop. (0 indexed)
- loop.revindex The number of iterations from the end of the loop (1 indexed)
- loop.revindex0 The number of iterations from the end of the loop (0 indexed)
- loop.first True if first iteration
- loop.last True if last iteration
- loop.length The number of items in the sequence
- loop.parent The parent context
{% for user in users %} {{ loop.index }} - {{ user.username }} {% endfor %}
The loop.length, loop.revindex, loop.revindex0, and loop.last variables are only available for PHP arrays, or objects that implement the Countable interface.