Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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
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 %}
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.
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!
@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 %}
In Canada, payment processors, like those that provide payment processing services t...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025