d
Topic
Posts:
18
4 months ago

Do not render if array variable is empty

Hi Guys

How can i improve this code, in order to make sure, that array variable is not rendered if it is empty:

{% assign foo = 'a,b,c' | split:',' %}
	1: {{ foo[0] }}
	2: {{ foo[1] }}
	3: {{ foo[2] }}
	4: {{ foo[3] }}

 

Cheers

Martynas

i
Replies
Jason Shopify Expert freakdesign.com.au
Posts:
7986
4 months ago

Since you've got a pattern in the output you may as well use a for loop.

{% assign foo = 'a,b,c' | split:',' %}
{% for bar in foo %}
  {% if bar == blank %}{% continue %}{% endif %}
  {{ forloop.index }}: {{ bar }}
{% endfor %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
18
4 months ago

Nice, thank you, Jason!

 

Martynas