How to identify the last loop of a specific block type in liquid?

How to identify the last loop of a specific block type in liquid?

MaxDesign
Shopify Partner
214 15 88

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

Reach out to me at admin@maxdesign.expert
Replies 4 (4)

Mircea_Piturca
Shopify Partner
1548 44 349

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 %}

Finally—Add variant descriptions to your products
MaxDesign
Shopify Partner
214 15 88

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.

Reach out to me at admin@maxdesign.expert

CommerceBureau
Shopify Partner
5 0 5

Hey @MaxDesign, did you end up finding a solution to this? I have the same issue where it appears the forloop.last doesn't appear to be picking up the close of the loop, or the if statement which checks for the block type. Any help would be awesome!

Blair | Director @ The Commerce Bureau
Helping brands launch, optimise and grow their Shopify store.
Need more help? Visit our website!
MaxDesign
Shopify Partner
214 15 88

@CommerceBureau you should be able to do it like this if we stick to the initial code structure I used:

{% assign type2_size = section.blocks | where: 'type', 'type2' | size %}
{% for block in section.blocks %}    
   {% if block.type == 'type1' %}
      Type 1 block
   {% elsif block.type == 'type2' %} 
      Type 2 block
      {% assign count = count | plus: 1 %}
      {% if count == type2_size %}
         Last loop of type2
      {% endif %}
   {% endif %}
{% endfor %}

 

Reach out to me at admin@maxdesign.expert