<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>

CC BY-SA 3.0
2015-04-07T19:58:00.710Z
2015-04-07T19:58:00.710Z
Aaron Berkowitz
<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>

CC BY-SA 3.0
2015-04-09T13:53:33.540Z
Yup I did need them to be chronological. Thanks so much for your answer. Worked out perfectly.
julzmon
CC BY-SA 3.0
2015-04-07T20:14:56.467Z
2015-04-07T20:28:21.300Z
2015-04-07T20:28:21.300Z
Mats Mikkel Rummelhoff
<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>

<templating>
What's the best way to increment a number of a class or id?
# 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%}
    
    ...
    
  {% endif %}
{% endfor %}

**Tags:** templating

---

## 2 answers

### Answer by username_2 on 2015-04-07. Score: 14 | Accepted answer

By using the default filter 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 %}
        
            ...
        
    {% 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, which is available inside the for loop:
{% for block in entry.news %}
    {% if block.type == "bodyBlock" %}
        {{ block.body }}
    {% elseif block.type == "codeBlock" %}
        
            ...
        
    {% 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.


---

