How to target first block in {% for block in section.blocks %} ?

Solved
Highlighted

Hi Shopify community,

I think it's all in the question. I have some vertical scrolling texts (in blocks) and I need to target the first block in order to duplicate it at the end of the block section.

I'm not sure if I should use the forloop.index, at least I don't see how.

 

Any tips appreciated.

Max

C'est simple comme bonjour ! Ou presque.
Need some help to customize your website? Message me anytime: 5euros.com/profil/maxdesign
0 Likes
Highlighted
Explorer
49 5 10

I think this should work

 

{% for block in section.blocks %}

 

    {% if forloop.first == true %}

        {% assign first_block = block %}
    {% endif %}

 

{{% endfor %}}

 

 

then you can use the first_block variable to duplicate the block at the end

H.Azzam
1 Like
Highlighted

Hi @H-Azzam 

Thanks for your help but it seems uncomplete or I could not make it work (beside there is a typo error : {{% endfor %}} => {% endfor %} )

What does {% assign first_block = block %} do exactly ? It's an assignment but it does nothing right ?

How should I use the first_block variable in order to call that first block ?

 

Thanks for your additional help.

 

C'est simple comme bonjour ! Ou presque.
Need some help to customize your website? Message me anytime: 5euros.com/profil/maxdesign
0 Likes
Highlighted
Explorer
49 5 10

This is an accepted solution.

Ya sorry for the typo

the assign line assigns the first block in the loop to the first_block variable.

so when you finish looping through all blocks you can then use first_block variable to access the first block just like when you use the block variable.

 

for example after the for loop you can use the first_block variable like so

{{ first_block.title }}

 

this will output the title of the first block

 

or if this is confusing you can ignore the code i sent you before and you can get the first block like so

{{ section.blocks[0] }}

and to access any property inside it you can use

section.blocks[0].title
H.Azzam
1 Like
Highlighted

Yes this is clear now, and the first solution would work better if, for instance, I want to target the last block and I don't know which number it is (with foorloop.last).

 

So for now I did:

 

{{ section.blocks[0].settings.myid }}

And it works properly, cool. Thanks for the quick help.

C'est simple comme bonjour ! Ou presque.
Need some help to customize your website? Message me anytime: 5euros.com/profil/maxdesign
1 Like
Highlighted
Explorer
49 5 10

Glad it worked,

You're welcome!

H.Azzam
0 Likes
Highlighted
Tourist
6 1 1

Hi, 

 

Could you please advise on how to exclude a certain block from the loop {%for block in section.blocks%} ? Let's say first or second, or both?

0 Likes
Highlighted

Hi @mibol 

To exclude a specific block, you could use forloop.index

For example:

 

{%for block in section.blocks%}
    {% unless forloop.index == 2 %} //exclude second block
          //do this, do that 
     {% endunless %}
{% endfor %}

You can find more about forloop.index here: Shopify Cheat Sheet 

C'est simple comme bonjour ! Ou presque.
Need some help to customize your website? Message me anytime: 5euros.com/profil/maxdesign
1 Like