<p><code>element.type</code> is not a simple string (add <code>{{dump(element.type)}}</code> to your loop to see this). What you should use is <code>element.type.handle</code>. So the assignment is:</p>

<pre><code>{% set prevElementType = element.type.handle %}
</code></pre>

<p>And the comparison (now of two strings, so no recursion) is:</p>

<pre><code>element.type.handle == prevElementType
</code></pre>

CC BY-SA 3.0
2015-06-23T17:18:17.267Z
2015-06-23T17:18:17.267Z
Marion Newlevant
<p>I'm running into a nesting level error when looping through matrix blocks, and I can't quite figure out what the problem is.</p>

<p>The problem arises when I use a variable to track the type of the previous element in the matrix. I want to be able to know within the sub-template that an element is the n<sup>th</sup> of the same type (for adding a container around some matrix block types to group them).</p>

<p>If I replace line 7 below with, eg. <code>{% set n = (element.type == 'product') ? n + 1 : 0 %}</code> (ie. a named element type rather than a variable) it works as expected, but as soon as I compare <code>element.type</code> to <code>prevElementType</code> I get a <code>Fatal error: Nesting level too deep - recursive dependency?</code> error. Some of the partial templates that may be included in the loop check <code>prevElementType</code>, but none of them modify it, so I can't see what's causing the recursion. I'd be grateful for any help with this.</p>

<pre><code>{% if entry.contentBlocks is not empty %}
  {% set prevElementType = false %}

  {% for element in entry.contentBlocks %}

  {# Let's use a counter for multiple blocks of the same type: #}
  {% set n = (element.type == prevElementType) ? n + 1 : 1 %}

  {# Include the partial for this block: #}
  {% include "_pageelements/" ~ element.type|lower %}

  {# Track this element's type for the next iteration: #}
  {% set prevElementType = element.type %}

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

CC BY-SA 3.0
2015-06-23T10:44:19.330Z
(Incidentally, I can get around the problem by explicitly listing the element types that might need a container - eg. `if (element.type == 'product' or element.type == ' ... ')`, but ideally I'd still like to be able to track "nth of type" generically, so I'd like to get to the bottom of this)
Nick F
CC BY-SA 3.0
2015-06-23T16:39:36.983Z
What does `{{dump(element.type)}}` show you? If it's not a simple string, you may want to use `element.type.handle`
Marion Newlevant
CC BY-SA 3.0
2015-06-23T17:13:23.587Z
Oh gosh, you're absolutely right. Because the comparison of `element.type` to a string worked, I assumed `element.type` was itself a simple string. If you put the above comment as an answer, I'll mark it accepted. Thanks!
Nick F
CC BY-SA 3.0
2015-06-23T17:21:15.253Z
Twig has an `===` operator for when you want to avoid those type coercions: http://twig.sensiolabs.org/doc/tests/sameas.html
Marion Newlevant
CC BY-SA 3.0
2015-06-23T17:54:26.333Z
Ah, thanks - that's also good to know. I always use `===` when writing code, but hadn't realised that option was available in Twig.
Nick F
<templating><matrix><forloop><error><loop>
Nesting level too deep when looping through matrix blocks
# Nesting level too deep when looping through matrix blocks

(Asked by: username_0 on 2015-06-23)

I'm running into a nesting level error when looping through matrix blocks, and I can't quite figure out what the problem is.
The problem arises when I use a variable to track the type of the previous element in the matrix. I want to be able to know within the sub-template that an element is the nth of the same type (for adding a container around some matrix block types to group them).
If I replace line 7 below with, eg. {% set n = (element.type == 'product') ? n + 1 : 0 %} (ie. a named element type rather than a variable) it works as expected, but as soon as I compare element.type to prevElementType I get a Fatal error: Nesting level too deep - recursive dependency? error. Some of the partial templates that may be included in the loop check prevElementType, but none of them modify it, so I can't see what's causing the recursion. I'd be grateful for any help with this.
{% if entry.contentBlocks is not empty %}
  {% set prevElementType = false %}

  {% for element in entry.contentBlocks %}

  {# Let's use a counter for multiple blocks of the same type: #}
  {% set n = (element.type == prevElementType) ? n + 1 : 1 %}

  {# Include the partial for this block: #}
  {% include "_pageelements/" ~ element.type|lower %}

  {# Track this element's type for the next iteration: #}
  {% set prevElementType = element.type %}

  {% endfor %}
{% endif %}

**Tags:** templating, matrix, forloop, error, loop

> **Comments:**
> **username_0**: (Incidentally, I can get around the problem by explicitly listing the element types that might need a container - eg. `if (element.type == 'product' or element.type == ' ... ')`, but ideally I'd still like to be able to track "nth of type" generically, so I'd like to get to the bottom of this)  
> **username_1**: What does `{{dump(element.type)}}` show you? If it's not a simple string, you may want to use `element.type.handle`  
> **username_0**: Oh gosh, you're absolutely right. Because the comparison of `element.type` to a string worked, I assumed `element.type` was itself a simple string. If you put the above comment as an answer, I'll mark it accepted. Thanks!  
> **username_1**: Twig has an `===` operator for when you want to avoid those type coercions: http://twig.sensiolabs.org/doc/tests/sameas.html  
> **username_0**: Ah, thanks - that's also good to know. I always use `===` when writing code, but hadn't realised that option was available in Twig.  

---

## 1 answers

### Answer by username_1 on 2015-06-23. Score: 4 | Accepted answer

element.type is not a simple string (add {{dump(element.type)}} to your loop to see this). What you should use is element.type.handle. So the assignment is:
{% set prevElementType = element.type.handle %}

And the comparison (now of two strings, so no recursion) is:
element.type.handle == prevElementType


---

