Programming

Shopify Liquid Nil: Purpose vs Blank and Truthy Empty Strings

Understand nil's role in Shopify Liquid as falsy alongside false, why empty strings "" and '0' are truthy, and best practices using blank and empty filters in themes and templates.

4 answers 1 view

In Shopify Liquid, what is the purpose of nil or null values if conditions can be checked using blank or false? Why are empty strings ("") and '0' considered truthy rather than falsy?

Truthy and Falsy Values in Shopify Liquid

Only nil and false are falsy; most other values, including empty strings, numbers (like 0), empty arrays, and EmptyDrop objects, are truthy.

Value Truthy Falsy
true Yes
false Yes
nil Yes
empty string "" Yes
Numbers (integer, float) Yes
Nonempty array Yes
Empty array Yes
Objects (handles) Yes
Empty object (EmptyDrop) Yes

Best Practices

  • Use blank to check strings (e.g., empty or whitespace-only).
  • Use empty to check objects, arrays, or collections.

Examples

liquid
{% if settings.featured_potions_title != blank -%}
 {{ settings.featured_potions_title }}
{%- else -%}
 No value for this setting has been selected.
{%- endif %}

{% unless pages.recipes == empty -%}
 {{ pages.recipes.content }}
{%- else -%}
 No page with this handle exists.
{%- endunless %}

In Shopify Liquid, nil (the equivalent of null) specifically flags missing or undefined values from the Shopify API or objects, making it the only non-boolean falsy value alongside false—that’s why you need it even when blank handles empty strings or false covers booleans. Empty strings "" and '0' stay truthy on purpose, keeping liquid templates simple so common values like settings or user inputs don’t unexpectedly hide content in shopify themes. Developers rely on blank for strings and empty for arrays to catch those “empty but present” cases without overcomplicating every if statement.


Contents


Understanding Truthy and Falsy Values in Shopify Liquid

Ever coded in JavaScript and got tripped up by how 0 or "" acts falsy? Shopify Liquid flips that script. Here, only two things are falsy: nil and false. Everything else—even an empty string, the number zero, or a barren array—counts as truthy.

This choice isn’t random. It streamlines shopify liquid for theme builders who deal with dynamic data from products, settings, or customer info. No more surprises where a blank setting hides your hero banner. Check the official truthy/falsy guide for the full rundown.

Here’s the breakdown in a table:

Value Truthy Falsy
true Yes
false Yes
nil Yes
Empty string "" Yes
Numbers (e.g., 0, 42.5) Yes
Nonempty array Yes
Empty array [] Yes
Objects/handles Yes
Empty object (EmptyDrop) Yes

Why design it this way? Simplicity. Liquid prioritizes “something exists, show it” over strict emptiness checks.


The Role of nil and false as Falsy in Liquid Templates

nil steps in when Shopify’s API returns nothing—like a product variant that doesn’t exist or a missing collection page. It’s not just “empty”; it’s absent. In a conditional like {% if product.variant %}{{ product.variant.price }}{% endif %}, nil keeps that block silent, preventing broken layouts.

false handles booleans straight-up, say from a theme setting like settings.show_sidebar toggled off. But here’s the kicker: without nil as falsy, you’d have to wrap every object access in extra filters. Imagine querying a non-existent page handle—Liquid returns an EmptyDrop object that’s truthy, so your if fires anyway. nil draws a hard line for true absences.

From the Shopify Liquid basics, this duo keeps liquid templates predictable. You might think, “Can’t blank or false cover it?” Nope—blank ignores nil (treats it as blank, sure, but doesn’t catch the underlying absence), and false is purely boolean.


Why Empty Strings ("") and '0' Are Truthy

Picture this: A merchant leaves a product description blank. In most languages, "" is falsy—poof, your template skips it. But in Shopify Liquid, that empty string is truthy. Same with '0' or even 0. Why? To avoid hiding valid data.

Shopify themes pull from merchant inputs, API responses, or metafields. An empty description might mean “no custom text, use default.” Making it falsy could wipe out sections unintentionally. The Liquid types docs explain: numbers and strings default to truthy unless explicitly nil or false.

It’s a deliberate nod to real-world e-commerce. Zero stock? Still show the product (maybe as sold out). Blank title? Render a fallback. This forces you to use targeted filters, building more robust shopify liquid code.


Using blank and empty Filters in Shopify Themes

nil has its place, but for everyday checks, lean on filters. blank catches empty strings, whitespace-only strings, nil, or false—perfect for text fields in shopify themes.

Try this for a settings check:

liquid
{% if settings.featured_potions_title != blank -%}
 {{ settings.featured_potions_title }}
{%- else -%}
 No value for this setting has been selected.
{%- endif %}

empty, meanwhile, targets arrays, collections, or objects like pages. An empty array [] is truthy raw, but == empty flips it:

liquid
{% unless pages.recipes == empty -%}
 {{ pages.recipes.content }}
{%- endunless %}

These shine in liquid templates handling Shopify API objects, where EmptyDrop (a safe stub for missing items) needs explicit probing.


Best Practices for Conditionals in Shopify Liquid

Don’t just if on raw variables—always filter. Start with blank for strings, empty for lists. Combine them: {% if collection.products != empty and settings.collection_banner != blank %}.

In shopify themes, nest safely: {% if product and product.title != blank %} guards against nil variants. Use unless for negatives—cleaner than if not.

Pro tip: Whitespace matters. {%- if -%} strips it, keeping HTML tidy. Test in theme editor; Liquid’s forgiving, but edge cases like numeric strings bite.

For collections from Shopify API, for loops auto-skip empties, but conditionals need empty.


Common Pitfalls and Real-World Examples

Pitfall one: Assuming "" is falsy. Your hero image vanishes on blank settings. Fix: != blank.

Two: EmptyDrop surprises. {% if customer %} runs even for guests (truthy stub). Use customer != null or blank.

Example with variants:

liquid
{% if product.selected_or_first_available_variant.price != blank %}
 <span>{{ product.selected_or_first_available_variant.price | money }}</span>
{% endif %}

Or pages:

liquid
{% assign recipe_page = pages.recipes %}
{% if recipe_page != blank %}
 {{ recipe_page.content }}
{% else %}
 No recipes yet.
{% endif %}

These patterns, pulled from Liquid docs, save headaches in production shopify liquid.


Sources

  1. Truthy and Falsy — Official explanation of falsy values (nil, false) in Liquid: https://shopify.github.io/liquid/basics/truthy-and-falsy/
  2. Liquid Basics — Core Shopify documentation on types, conditionals, and filters: https://shopify.dev/docs/api/liquid/basics
  3. Liquid Types — Details on nil, EmptyDrop, strings, and arrays in templates: https://shopify.github.io/liquid/basics/types/

Conclusion

nil carves out space for true absences in Shopify Liquid, complementing false while blank and empty handle the nuanced empties that stay truthy—like "" or '0'—to keep shopify themes reliable and merchant-friendly. Embrace explicit checks; it’ll make your liquid templates bulletproof. Next time a conditional ghosts you, remember: it’s by design, not a bug.

In Shopify Liquid, only nil and false are falsy values; all others, including empty strings "", numbers like 0, empty arrays, and EmptyDrop objects, are truthy. This design simplifies liquid template logic, where non-boolean values in conditionals evaluate as true by default. For example, {% if page.category %} renders empty tags if page.category is an empty string, since it’s truthy. Use explicit checks like blank for strings to handle emptiness properly in Shopify themes.

Shopify Liquid uses nil as the sole representation of undefined/missing values that evaluate falsy in conditionals, distinct from false. Empty strings "", '0', and zero are truthy to keep the language simple—requiring blank for whitespace/empty strings and empty for collections/objects. Example: {% if settings.featured_potions_title != blank %} safely renders Shopify theme settings, preventing output of unintended empty content in liquid templates.

**nil** in Shopify Liquid is a special non-string value for no results, rendering nothing and falsy in if blocks, unlike truthy empty strings or EmptyDrop from non-existent objects. Check arrays/collections with == empty and use unless pages == empty for safe access in Shopify themes. Numbers, booleans, and arrays (even empty) are truthy, enforcing explicit filters like blank or empty in liquid template conditionals.

Authors
Sources
Documentation Portal
E-commerce Platform
Verified by moderation
NeuroAnswers
Moderation
Shopify Liquid Nil: Purpose vs Blank and Truthy Empty Strings