<p>Do you mean to have it just assign them one after the other? If so, then you can just use <a href="https://twig.symfony.com/doc/2.x/functions/cycle.html" rel="nofollow noreferrer">Twig’s <code>cycle</code> function</a>.</p>

<pre><code>{% set themes = [
    'yellow',
    'red',
    'teal',
    'blue'
] %}

{% for region in regions %}
 &lt;a href="{{ region.url }}" class="text-{{ cycle(themes, loop.index) }}"&gt;{{ region.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-12-02T20:31:06.787Z
This worked: `{{ cycle(themes, loop.index) }}`. Thanks, mate!
Jay
CC BY-SA 4.0
2019-12-02T20:38:09.877Z
Ah, good catch! That’ll teach me to write from my meatbrain and not refer to the docs. I’ve updated the response accordingly, including documentation.
Christopher Kennedy-Nuñez
CC BY-SA 4.0
2019-12-04T16:22:10.743Z
Another update, the array starts at 0 whilst the loop started at 1... so `loop.index - 1` was needed to start from the start. ‍♂️
Jay
CC BY-SA 4.0
2019-12-04T21:40:37.480Z
Ah, true. You don’t need to use math, though. You can just use `loop.index0` instead!
Christopher Kennedy-Nuñez
CC BY-SA 4.0
2019-12-02T20:08:42.710Z
2019-12-02T20:35:19.363Z
2019-12-02T20:35:19.363Z
Christopher Kennedy-Nuñez
<p>I have a loop of a list of regions on a project, this could be an infinite list of areas as my client's customer-base grows.</p>

<p>The project branding has four key colours, I have these in an array:</p>

<pre><code>{% set themes = [
    'yellow',
    'red',
    'teal',
    'blue'
] %}
</code></pre>

<p>What I'm looking to do is grab one of these themes as I loop over my regions with a basic <code>{% for region in regions %}</code>, then on the 5th, 9th and so on loop jump back to #1.</p>

<templating><loop><array>
Reuse items from array with forloop
# Reuse items from array with forloop

(Asked by: username_0 on 2019-12-02)

I have a loop of a list of regions on a project, this could be an infinite list of areas as my client's customer-base grows.
The project branding has four key colours, I have these in an array:
{% set themes = [
    'yellow',
    'red',
    'teal',
    'blue'
] %}

What I'm looking to do is grab one of these themes as I loop over my regions with a basic {% for region in regions %}, then on the 5th, 9th and so on loop jump back to #1.

**Tags:** templating, loop, array

---

## 1 answers

### Answer by username_1 on 2019-12-02. Score: 2 | Accepted answer

Do you mean to have it just assign them one after the other? If so, then you can just use Twig’s cycle function.
{% set themes = [
    'yellow',
    'red',
    'teal',
    'blue'
] %}

{% for region in regions %}
 {{ region.title }}
{% endfor %}

> **Comments:**
> **username_0**: This worked: `{{ cycle(themes, loop.index) }}`. Thanks, mate!  
> **username_1**: Ah, good catch! That’ll teach me to write from my meatbrain and not refer to the docs. I’ve updated the response accordingly, including documentation.  
> **username_0**: Another update, the array starts at 0 whilst the loop started at 1... so `loop.index - 1` was needed to start from the start. ‍♂️  
> **username_1**: Ah, true. You don’t need to use math, though. You can just use `loop.index0` instead!  

---

