<p>Not really sure what you're wanting the dynamic portion to be based on, but if it's something like the <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">loop count</a>, then you can do something like this:</p>

<pre><code>{% set tags = slide.foodTags %}

{% if tags %} 

    &lt;div&gt;   
        {% for tag in tags %}                     
            &lt;span class="dynamic-{{ loop.index }}"&gt;
                {{ tag.title }}
            &lt;/span&gt;
        {% endfor %}
    &lt;/div&gt; 

{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-17T08:03:02.910Z
I wanted do loop count, but I only knew method `{% for i in 1..entries|length %}`. I couldn't put it to my model, but now I can do it with `{{ loop.index }}`, which I had no idea, It does such great thing. Thank you @Brad Bell
Dominik Krulak
CC BY-SA 3.0
2015-03-17T12:10:21.380Z
Be advised the ```loop``` variable contains other useful properties as well, such as _first_, _last_ and _odd_. If you needed to style the first item different from the rest, for instance, you could write ```class="{{ loop.first ? 'first' }}"``` – or if you needed to style odd/even items you could write ```class="{{ loop.odd ? 'odd' : 'even' }}"```.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-16T23:12:30.110Z
2015-03-16T23:12:30.110Z
Brad Bell
<p>I can't comment yet to reply directly to Mats Mikkel Rummelhoff's comment above, but just in case someone comes across this and is looking to alternate styles instead of basing it on the loop.index count.</p>

<p>You would do so using the Twig odd or even tests</p>

<pre><code>{{ loop.index is odd ? 'odd' : 'even' }}
</code></pre>

<p>or</p>

<pre><code>{{ loop.index is even ? 'even' : 'odd' }}
</code></pre>

<p>There is no loop.odd key</p>

CC BY-SA 3.0
2017-05-09T03:51:46.650Z
So you meant to comment and wound up leaving a perfectly acceptable answer? Nice work, MalTek! :)
Matt Stein
CC BY-SA 3.0
2017-05-10T04:50:28.440Z
:) I went to leave a comment, but it said I needed 50 points before I could. But I was allowed to add an answer. It was the second time I had come across this thread and tried loop.odd, so thought it might have happened to others! (and looking out for my future self, haha)
MalTek
CC BY-SA 3.0
2017-05-08T19:56:28.633Z
2017-05-08T19:56:28.633Z
MalTek
<p>I have this simple element model and I'm trying to add dynamic class within loop.
So every time a model outputs, It outputs dynamic class. Can't go for another loop within loop, in case I could, therefor It'd be easy.</p>

<pre><code>            {% set tags = slide.foodTags %}
            {% if tags %} 

            &lt;div&gt;   
                  {% for tag in tags %}                     
                        &lt;span class="{# dynamic-class #}"&gt;
                            {{ tag.title }}
                        &lt;/span&gt;
                  {% endfor %}
            &lt;/div&gt; 

           {% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-16T21:08:25.753Z
What do you want the dynamic class to be based on? The tag's ID? It's position in the loop? An element related to the tag?
Peter Tell
CC BY-SA 3.0
2015-03-17T07:59:41.250Z
Based on count. `{{ loop.index }}` helped. Thanks for concern @Peter Tell
Dominik Krulak
<forloop>
Set dynamic class in for Loop
# Set dynamic class in for Loop

(Asked by: username_0 on 2015-03-16)

I have this simple element model and I'm trying to add dynamic class within loop.
So every time a model outputs, It outputs dynamic class. Can't go for another loop within loop, in case I could, therefor It'd be easy.
            {% set tags = slide.foodTags %}
            {% if tags %} 

               
                  {% for tag in tags %}                     
                        
                            {{ tag.title }}
                        
                  {% endfor %}
             

           {% endif %}

**Tags:** forloop

> **Comments:**
> **username_1**: What do you want the dynamic class to be based on? The tag's ID? It's position in the loop? An element related to the tag?  
> **username_0**: Based on count. `{{ loop.index }}` helped. Thanks for concern @username_1  

---

## 2 answers

### Answer by username_2 on 2015-03-16. Score: 3 | Accepted answer

Not really sure what you're wanting the dynamic portion to be based on, but if it's something like the loop count, then you can do something like this:
{% set tags = slide.foodTags %}

{% if tags %} 

       
        {% for tag in tags %}                     
            
                {{ tag.title }}
            
        {% endfor %}
     

{% endif %}

> **Comments:**
> **username_0**: I wanted do loop count, but I only knew method `{% for i in 1..entries|length %}`. I couldn't put it to my model, but now I can do it with `{{ loop.index }}`, which I had no idea, It does such great thing. Thank you @username_2  
> **username_3**: Be advised the ```loop``` variable contains other useful properties as well, such as _first_, _last_ and _odd_. If you needed to style the first item different from the rest, for instance, you could write ```class="{{ loop.first ? 'first' }}"``` – or if you needed to style odd/even items you could write ```class="{{ loop.odd ? 'odd' : 'even' }}"```.  

---

### Answer by username_4 on 2017-05-08. Score: 5

I can't comment yet to reply directly to username_3's comment above, but just in case someone comes across this and is looking to alternate styles instead of basing it on the loop.index count.
You would do so using the Twig odd or even tests
{{ loop.index is odd ? 'odd' : 'even' }}

or
{{ loop.index is even ? 'even' : 'odd' }}

There is no loop.odd key

> **Comments:**
> **username_5**: So you meant to comment and wound up leaving a perfectly acceptable answer? Nice work, username_4! :)  
> **username_4**: :) I went to leave a comment, but it said I needed 50 points before I could. But I was allowed to add an answer. It was the second time I had come across this thread and tried loop.odd, so thought it might have happened to others! (and looking out for my future self, haha)  

---

