forloop.last of a specific block type

Highlighted

Hi,

How to check if it's the last loop of a specific block type ? Here is what my code structure look like but it does not work.

 

{% for block in section.blocks %}    
      {% if block.type == 'type1' %}
        Type 1 blocks in a loop
      {% elsif block.type == 'type2' %} 
Type 2 blocks in a loop {% if forloop.last == true and block.type == 'type2' %} last loop of type 2 {% endif %} {% endif %} {% endfor %}


I'm learning a bit more of liquid everyday, I'd really appreciate some help.

Regards

C'est simple comme bonjour ! Ou presque.
Need some help to customize your website? Message me anytime: 5euros.com/profil/maxdesign
0 Likes
Shopify Partner
1361 21 195

Hey Max,

You code can fail as one block that has "type2" may not be the last block.

 

I am not sure what info you need to pull from that last block (with type2).

You could look into:

https://help.shopify.com/en/themes/liquid/filters/array-filters#map

https://help.shopify.com/en/themes/liquid/filters/array-filters#where

 

of with a lame approach:

{% for block in section.blocks %}    
  {% if block.type == 'type2' %}
    {% assign last_type2 = block %}
  {% endif %}
 {% endfor %}

https://sections.design tips, tricks & Shopify sections
0 Likes

Hi Mircea and thank you for your help,

 

"You code can fail as one block that has "type2" may not be the last block." 

 

But can't we know with liquid the very last block of a specific type?

 

I don't understand how 

{% assign last_type2 = block %}

can help me know I'm in the last loop of block type2? There must be some missing pieces here, and honestly I can't figure out how to find it. 


What I need is to add a different piece of html only in the last block of type2, but I don't see how the filters #map and #where can help do that. Ultimately I used javascript, but I'd be surprised if liquid can't handle it.

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