What's the best way to increment a number of a class or id? 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%}
    &lt;div id="accordion{{ counter }}"&gt;
    ...
    &lt;/div&gt;
  {% endif %}
{% endfor %}
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 %}
        &lt;div id="accordion{{ counter }}"&gt;
            ...
        &lt;/div&gt;
    {% 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" %}
        &lt;div id="accordion{{ loop.index }}"&gt;
            ...
        &lt;/div&gt;
    {% endif %}
{% endfor %}