How to traverse theme block

How to traverse theme block.
the code in Below, is not Works.

{% for block in content_for ‘blocks’ %}

{{ block }}

{% endfor %}

Hi, that is not valid liquid. You should just be using the following below, you do not need to loop over the blocks.

{% content_for 'blocks' %}

i want to use theme blocks in swiper,so i need use for… in … to render it
just like

{% for block in content_for ‘blocks’ %}


{{ block }}

{% endfor %}

You should probably look into a combination of dynamic + static blocks.
You can read more here: Static Blocks

it’s not this, and i use render solve it , thanks.
And i have another quesion,
how can i control a theme block can not choose itself?

Define the exact block types you want to enable in that block.
i.e.

"blocks": [{ "type": "yourcustomblock" }]

instead of

"blocks": [{ "type": "@theme" }]

As others have mentioned above, you can render theme blocks using the {% content_for "blocks" %} tag.

We strongly encourage you use {% content_for "blocks" %} to render theme blocks.

Alternatively, if you have an advanced use case, you can iterate over theme blocks using a for loop. Since the markup for theme blocks lives in a different file, you have to delegate rendering by passing the block drop to the render tag:

Example 1: Manually rendering theme blocks from a section:

{% for block in section.blocks %}
  {% render block %}
{% endfor %}

Example 2: Manually rendering theme blocks from another theme block, in reverse order:

{% assign reversed_blocks = block.blocks | reverse %}

{% for block in reversed_blocks %}
  {% render block %}
{% endfor %}
1 Like

Hi Clauderic!
Glad to see the manual rendering still works, but I wonder how we would be able to selectively render theme blocks as we did with locally defined ones.

E.g. filtering blocks with “| where: ‘type’”, or using {%- case block.type -%}.
As of now, block.type always returns ‘@theme’ for theme blocks.

What would you advise?

This is a very important topic and Shopify Dev Team should address this as well this, before launching Horizon…

This isn’t necessarily a problem with Horizon, its the theme blocks feature itself.

Technically speaking yes. I refer it’s an Horizon problem because if you launch a new theme framework where the big news is nested blocks, you must give the ability to move blocks. Especially if, like in the examples presented by Shopify, they present “block groups” as a great feature to organize blocks into layout (rows, columns etc.). I am writing this because the idea is just perfect but it’s missing one very crucial feature. In my case I cannot start using nested blocks just for this reason. Also you cannot rearrange blocks between sections, despite Shopify documentation says it’s possible: https://help.shopify.com/en/manual/online-store/themes/theme-structure/sections-and-blocks … " You can change the order of sections on your page templates and rearrange blocks within those sections. Additionally, you can move compatible blocks between different sections. For example, if your homepage features two Slideshow sections, then you can drag a Slide block from one slideshow to the other." IT DOESN’T WORK!

Follow up. Asking for Shopify engineers reply.

You can change the order of sections on your page templates and rearrange blocks within those sections. Additionally, you can move compatible blocks between different sections. For example, if your homepage features two Slideshow sections, then you can drag a Slide block from one slideshow to the other." IT DOESN’T WORK!

Can confirm, it did indeed work for me when I tried yesterday. Maybe not released for everyone yet.

Not working actually, just tried with the new “HERITAGE” theme.

Does not for me either. Maybe a rollback?