Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-17T08:03:02.910Z
Answers.Text: 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
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-17T12:10:21.380Z
Answers.Text: 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' }}"```.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-16T23:12:30.110Z
Answers.LastActivityDate: 2015-03-16T23:12:30.110Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-09T03:51:46.650Z
Answers.Text: So you meant to comment and wound up leaving a perfectly acceptable answer? Nice work, MalTek! :)
Answers.UserDisplayName: Matt Stein
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-10T04:50:28.440Z
Answers.Text: :) 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)
Answers.UserDisplayName: MalTek
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-08T19:56:28.633Z
Answers.LastActivityDate: 2017-05-08T19:56:28.633Z
Answers.UserDisplayName: MalTek
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-16T21:08:25.753Z
Comments.Text: 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?
Comments.UserDisplayName: Peter Tell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-03-17T07:59:41.250Z
Comments.Text: Based on count. `{{ loop.index }}` helped. Thanks for concern @Peter Tell
Comments.UserDisplayName: Dominik Krulak
Tags: <forloop>
Title: Set dynamic class in for Loop
ThreadText: # 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 %} 
  
              <div>   
                    {% for tag in tags %}                     
                          <span class="{# dynamic-class #}">
                              {{ tag.title }}
                          </span>
                    {% endfor %}
              </div> 
  
             {% 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](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable), then you can do something like this:
  
  ```
  {% set tags = slide.foodTags %}
  
  {% if tags %} 
  
      <div>   
          {% for tag in tags %}                     
              <span class="dynamic-{{ loop.index }}">
                  {{ tag.title }}
              </span>
          {% endfor %}
      </div> 
  
  {% 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)  
  
  ---
  
  