I'm using ACF and Timber (twig) for Wordpress.
I'm trying to group names in group of letters. For example :
**A-E**
Arno
Adrieno
Arvay
Emily
Etienne
**F-J**
Fanny
Franck
Fergy
**K-O**
etc..
**0-9**
I succeeded in doing an alphabetical classification of an ACF repeater field.
$index_list = get_field('index_list_repeater');
foreach ($index_list as $key => $row) {
$the_name[$key] = $row;
}
array_multisort($the_name, SORT_ASC, $index_list);
$context['index_list'] = $index_list;
But how can I group names by letter? How would I add a range (A-E, F-J etc.) with Twig?
{% for item in index_list|range(A,E) %}
<p>{{ item.index_name }}</p>
{% endfor %}
That's easy, you just need to go through two loops. For example A-L will look like this:
{% for letter in range('a', 'l') %}
{% for item in index_list %}
{% if item.index_name|first|lower == letter %}
{{ item.index_name }}
{% endif %}
{% endfor %}
{% endfor %}
Update #2: For numbers it will look like:
{% set index_list = [{ index_name: 'Alex'}, { index_name: 'lola' }, { index_name: '5 Master' }, { index_name: '2 Master' } ] %}
{% for letter in range(0, 9) %}
{% for item in index_list %}
{% if item.index_name|first|lower == letter|lower %}
{{ item.index_name }}
{% endif %}
{% endfor %}
{% endfor %}