Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is if statement necessary for empty array situation?

I'm using Shopify Liquid. If I don't include {% if my_array %} and have a code such as this:

{% for var in my_array %}
Do this heavy task
{% endif %}

does it skip the "heavy task" if my_array is empty or is it better to include the if statement (performance wise)? Thank you.

like image 558
RD2PB Avatar asked Oct 20 '25 13:10

RD2PB


1 Answers

Thank you for your answers, but I found this from Shopify developers documents. According to Shopify, {% for var in my_array %} also acts like an if statement, which can be combined with {% else %} for when the array is empty. for example:

{% for var in my_array %}
Do this heavy task
{% else %}
<p>This array is empty</p>
{% endfor %}

Hope this helps others searching for it too.

like image 153
RD2PB Avatar answered Oct 23 '25 07:10

RD2PB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!