Solved

Conditional logic with for loops

honeybadgermse
Excursionist
19 0 6

Hi all,

I have been scratching my head around the best way to approach this design, I have a block which is limited to 3 but in the for loop, I have been trying to do some conditional logic to count the first block coming out, then I need to wrap the other two blocks in a div so we have a 60 / 40 Flex on the two-child divs.

What's the best way to approach this with liquid? I have tried like so but everything is coming out as "full_width"

 

 

{% assign count = 0 %}

	{% for block in section.blocks %}  
	{% assign count = count | plus: 1 %}

	{% assign   full_width = "full_width" %}
	{% assign   fifty = "fifty" %}

	{% if count > 0 %}
	{{full_width}}
	{% else count > 1 %}
	{{fifty}}
	{% endif %} 

	<div class="block_{{ count }}">My element with counter class</div>

{% endfor %}

 

 

loop.jpg

Accepted Solution (1)

Hardik29418
Shopify Partner
2913 419 1081

This is an accepted solution.

{% assign count = 0 %}

	{% for block in section.blocks %}  
	{% assign count = count | plus: 1 %}

	{% assign   full_width = "full_width" %}
	{% assign   fifty = "fifty" %}

	{% if count > 1 %}
           {{fifty}}
	{% elsif count == 1 %}
           {{full_width}}	
	{% endif %} 

	<div class="block_{{ count }}">My element with counter class</div>

{% endfor %}

- Need a Shopify developer? Chat on WhatsApp or EMAIL ME!


- Your Coffee Tip would do Magic code ❤️
- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
- Email

View solution in original post

Replies 4 (4)

Hardik29418
Shopify Partner
2913 419 1081

This is an accepted solution.

{% assign count = 0 %}

	{% for block in section.blocks %}  
	{% assign count = count | plus: 1 %}

	{% assign   full_width = "full_width" %}
	{% assign   fifty = "fifty" %}

	{% if count > 1 %}
           {{fifty}}
	{% elsif count == 1 %}
           {{full_width}}	
	{% endif %} 

	<div class="block_{{ count }}">My element with counter class</div>

{% endfor %}

- Need a Shopify developer? Chat on WhatsApp or EMAIL ME!


- Your Coffee Tip would do Magic code ❤️
- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
- Email
honeybadgermse
Excursionist
19 0 6

Oh dear, I feel silly now, sorry I am new to Liquid and generally work with PHP but yes, of course, it's always going to be 0. 

Thank you @Hardik29418  🙂

honeybadgermse
Excursionist
19 0 6
Click to expand...
I suppose then the best approach for this is to use cycle and a mix of the if statements right?
Hardik29418
Shopify Partner
2913 419 1081

Yes, And please check my messages also

- Need a Shopify developer? Chat on WhatsApp or EMAIL ME!


- Your Coffee Tip would do Magic code ❤️
- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
- Email