skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    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.


  2. Since you’re writing it in liquid, you won’t see any performance issues since the result will already be written when the page loads.

    Login or Signup to reply.
  3. If you only need to check whether it is empty or not

    {% if my_array !== blank %}
    Do this heavy task
    {% else %}
    <p>This array is empty</p>
    {% endif %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search