Handling empty values

If a value of a custom field is empty, its corresponding metafield does not exist. 

A test for that particular custom field will not return anything ("nil" in Liquid terms) and which gives you a nice clean syntax for checking if a given field has a value:

{% if product.metafields.accentuate.title %}
   <h1>{{ product.metafields.accentuate.title }}</h1> 
{% endif %}

This is a little different for repeatable fields. If you are testing to see if an element in a repeatable field has a value, you need to test if the element's value is either an empty string or null, both of which can be tested via the length of the element's value:

{% for ingredient in product.metafields.accentuate.ingredients %}
    {% if ingredient.size > 0 %}
     <p>{{ ingredient }}</p>
   {% endif %}
{% endfor %}

And, if you are testing to see if a repeatable field has any elements at all:

{% if product.metafields.accentuate.ingredients.size == 0 %}
   <p>No ingredients have been added</p>
{% endif %}

Please see https://shopify.github.io/liquid/filters/size/ for details on using the size filter.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us