Craft CMS Asked by Lukas Snarskis on March 16, 2021
My entry content is displayed like this:
{% for block in entry.postContent.all() %}
<div class="my-4">
{% if block.type == 'text' %}
{{ block.text }}
{% elseif block.type == 'image' %}
{% for image in block.image.all() %}
<img src="{{ image.url }}" alt="{{ image.title }}" />
{% endfor %}
{% endif %}
</div>
{% 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?
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 %}
<div class="my-4">
{# 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() %}
<img src="{{ image.url }}" alt="{{ image.title }}" />
{% endfor %}
{% endif %}
</div>
{% endfor %}
Answered by Oli on March 16, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP