Answers.Body: <p>There is no simple <code>counter++</code> in twig, so your solution is a good way to handle this. What are you trying to do with the counter variable? There are other functions in twig like <a href="http://twig.sensiolabs.org/doc/functions/cycle.html" rel="nofollow">cycle</a> that are often useful in these situations.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T19:58:00.710Z
Answers.LastActivityDate: 2015-04-07T19:58:00.710Z
Answers.UserDisplayName: Aaron Berkowitz
Answers.Body: <p>By using the <a href="http://twig.sensiolabs.org/doc/filters/default.html"><em>default</em> filter</a> you can get away with a single line of Twig to set <em>and</em> increment the counter:</p>
  
  <pre><code>{% for block in entry.news %}
      {% if block.type == "bodyBlock" %}
          {{ block.body }}
      {% elseif block.type == "codeBlock" %}
          {% set counter = ( counter | default(0) ) + 1 %}
          &lt;div id="accordion{{ counter }}"&gt;
              ...
          &lt;/div&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>Also, I'm guessing you need the accordion elements to be chronologically numbered, but if you <em>don't</em>, you can use the <code>index</code> property of the <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable">loop variable</a>, which is available inside the <code>for</code> loop:</p>
  
  <pre><code>{% for block in entry.news %}
      {% if block.type == "bodyBlock" %}
          {{ block.body }}
      {% elseif block.type == "codeBlock" %}
          &lt;div id="accordion{{ loop.index }}"&gt;
              ...
          &lt;/div&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-09T13:53:33.540Z
Answers.Text: Yup I did need them to be chronological. Thanks so much for your answer. Worked out perfectly.
Answers.UserDisplayName: julzmon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-07T20:14:56.467Z
Answers.LastActivityDate: 2015-04-07T20:28:21.300Z
Answers.LastEditDate: 2015-04-07T20:28:21.300Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I have the following code which works fine. I'm just wondering if there is a more simple way of incrementing the id number?</p>
  
  <pre><code>{% set counter = '1' %}
  {% for block in entry.news %}
    {% if block.type == "bodyBlock" %}
      {{ block.body }}
    {% elseif block.type == "codeBlock" %}
    {% set counter = counter + 1%}
      &lt;div id="accordion{{ counter }}"&gt;
      ...
      &lt;/div&gt;
    {% endif %}
  {% endfor %}
  </code></pre>
  
Tags: <templating>
Title: What's the best way to increment a number of a class or id?
ThreadText: # What's the best way to increment a number of a class or id?
  
  (Asked by: username_0 on 2015-04-07)
  
  I have the following code which works fine. I'm just wondering if there is a more simple way of incrementing the id number?
  
  ```
  {% set counter = '1' %}
  {% for block in entry.news %}
    {% if block.type == "bodyBlock" %}
      {{ block.body }}
    {% elseif block.type == "codeBlock" %}
    {% set counter = counter + 1%}
      <div id="accordion{{ counter }}">
      ...
      </div>
    {% endif %}
  {% endfor %}
  
  ```
  
  **Tags:** templating
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-04-07. Score: 14 | Accepted answer
  
  By using the [*default* filter](http://twig.sensiolabs.org/doc/filters/default.html) you can get away with a single line of Twig to set *and* increment the counter:
  
  ```
  {% for block in entry.news %}
      {% if block.type == "bodyBlock" %}
          {{ block.body }}
      {% elseif block.type == "codeBlock" %}
          {% set counter = ( counter | default(0) ) + 1 %}
          <div id="accordion{{ counter }}">
              ...
          </div>
      {% endif %}
  {% endfor %}
  
  ```
  
  Also, I'm guessing you need the accordion elements to be chronologically numbered, but if you *don't*, you can use the `index` property of the [loop variable](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable), which is available inside the `for` loop:
  
  ```
  {% for block in entry.news %}
      {% if block.type == "bodyBlock" %}
          {{ block.body }}
      {% elseif block.type == "codeBlock" %}
          <div id="accordion{{ loop.index }}">
              ...
          </div>
      {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Yup I did need them to be chronological. Thanks so much for your answer. Worked out perfectly.  
  
  ---
  
  ### Answer by username_1 on 2015-04-07. Score: 2
  
  There is no simple `counter++` in twig, so your solution is a good way to handle this. What are you trying to do with the counter variable? There are other functions in twig like [cycle](http://twig.sensiolabs.org/doc/functions/cycle.html) that are often useful in these situations.
  
  
  ---
  
  