<p>There is a <code>loop</code> variable available within your <code>for</code> loop which you might be able to use here (<a href="https://twig.symfony.com/doc/3.x/tags/for.html#the-loop-variable" rel="nofollow noreferrer">docs</a>). This starts at 1 and increment everytime your loop <em>runs</em>.</p>
<p>Using <code>loop.index</code>, you could do something like this:</p>
<pre><code>{# Fetch your blocks #}
{% set blocks = entry.postContent.all() %}

{# Counts how many blocks you have in total #}
{% set totalBlocks = blocks | length %}

{# Divide your totalBlocks by 2 and round to find the 50% mark in your content #}
{% set halfwayPoint = (totalBlocks / 2) | round %}

{% for block in blocks %}
    &lt;div class=&quot;my-4&quot;&gt;

        {# When you reach halfway, inject #}
        {% if loop.index == halfwayPoint %}
            the content you want to inject
        {% endif %}

        {% if block.type == 'text' %}
            {{ block.text }}
        {% elseif block.type == 'image' %}
            {% for image in block.image.all() %}
                &lt;img src=&quot;{{ image.url }}&quot; alt=&quot;{{ image.title }}&quot; /&gt;
            {% endfor %}
        {% endif %}
    &lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2021-03-11T18:57:31.627Z
It doesn't work since we put all the content in one block. My idea was to use <p> tags that seperate every paragraph. Maybe it's possible to make it work in this way?
Lukas Snarskis
CC BY-SA 4.0
2021-05-24T21:54:04.057Z
I'm still looking for solution how to count <p> elements from matrix blocks and display in the middle. So far I went to this: `{% set blocks = entry.postContent.all() %}
{% set block = blocks[0] ?? null %}
{% set count = block.text | substringCount('<p>') %}
{% set halfwayPoint = (count / 2) | round %} ` I get half number of <p> but cannot get content displayed after that...
Lukas Snarskis
CC BY-SA 4.0
2021-03-11T17:54:35.020Z
2021-03-11T17:54:35.020Z
Oli
<p>My entry content is displayed like this:</p>
<pre><code>{% for block in entry.postContent.all() %}
  &lt;div class=&quot;my-4&quot;&gt;
        {% if block.type == 'text' %}
      {{ block.text }}
    {% elseif block.type == 'image' %}
      {% for image in block.image.all() %}
        &lt;img src=&quot;{{ image.url }}&quot; alt=&quot;{{ image.title }}&quot; /&gt;
      {% endfor %}
    {% endif %}
  &lt;/div&gt;
{% endfor %}
</code></pre>
<p>I want to display some other things in the middle of this content but have no idea how I can do it. Maybe someone have any thoughts?</p>

CC BY-SA 4.0
2021-03-11T12:43:53.843Z
You seem to only call 1 block here (`limit(1)`) so where do you need to inject content exactly?
Oli
CC BY-SA 4.0
2021-03-11T13:10:08.297Z
My bad.. I tried put limit over there but it doesn't change anything. That code displays my content. Everything is one block so I was thinking maybe I can count <p> somehow and after 50% of them insert lines that I want..
Lukas Snarskis
<matrix><content>
Display something in the middle of post content
# Display something in the middle of post content

(Asked by: username_0 on 2021-03-11)

My entry content is displayed like this:
{% for block in entry.postContent.all() %}
  
        {% if block.type == 'text' %}
      {{ block.text }}
    {% elseif block.type == 'image' %}
      {% for image in block.image.all() %}
        
      {% endfor %}
    {% endif %}
  
{% endfor %}

I want to display some other things in the middle of this content but have no idea how I can do it. Maybe someone have any thoughts?

**Tags:** matrix, content
> **Comments:**
> **username_1**: You seem to only call 1 block here (`limit(1)`) so where do you need to inject content exactly?  
> **username_0**: My bad.. I tried put limit over there but it doesn't change anything. That code displays my content. Everything is one block so I was thinking maybe I can count <p> somehow and after 50% of them insert lines that I want..  

---

## 1 answers

### Answer by username_1 on 2021-03-11. Score: 1

There is a loop variable available within your for loop which you might be able to use here (docs). This starts at 1 and increment everytime your loop runs.
Using loop.index, you could do something like this:
{# Fetch your blocks #}
{% set blocks = entry.postContent.all() %}

{# Counts how many blocks you have in total #}
{% set totalBlocks = blocks | length %}

{# Divide your totalBlocks by 2 and round to find the 50% mark in your content #}
{% set halfwayPoint = (totalBlocks / 2) | round %}

{% for block in blocks %}
    

        {# When you reach halfway, inject #}
        {% if loop.index == halfwayPoint %}
            the content you want to inject
        {% endif %}

        {% if block.type == 'text' %}
            {{ block.text }}
        {% elseif block.type == 'image' %}
            {% for image in block.image.all() %}
                
            {% endfor %}
        {% endif %}
    
{% endfor %}

> **Comments:**
> **username_0**: It doesn't work since we put all the content in one block. My idea was to use <p> tags that seperate every paragraph. Maybe it's possible to make it work in this way?  
> **username_0**: I'm still looking for solution how to count <p> elements from matrix blocks and display in the middle. So far I went to this: `{% set blocks = entry.postContent.all() %} {% set block = blocks[0] ?? null %} {% set count = block.text | substringCount('<p>') %} {% set halfwayPoint = (count / 2) | round %} ` I get half number of <p> but cannot get content displayed after that...  

---

