Liquid Updates

Jonathan Rudenberg

Over the past few days we've rolled out several new features to liquid templates. These are the additions:

for...else condition

Currently it's pretty common to see liquid markup with a for loop nested in an if condition like this:

{% if cart.item_count > 0 %}
  {% for item in cart.items %}
    <!-- display items -->
  {% endfor %}
{% else %}
  no items in your cart.
{% endif %}

Now, that markup can be simplified to this:

{% for item in cart.items %}
  <!-- display items -->
{% else %}
  no items in your cart.
{% endfor %}

If the object that the for tag is looping through is empty, the optional else condition will be used instead.

split filter

The new split filter makes it easy to display parts of a string. For example, to have a different product description on the collection and product pages:

In collection.liquid:

{{ product.description | split: '<!-- split -->' | first }}

In product.liquid:

{{ product.description | split: '<!-- split -->' | last }}

New Variables

We have also exposed several new variables to liquid templates.

The cart is now accessible to the json filter, which allows you to power all sorts of Javascript logic easily.

{{ cart | json }}

We have also exposed customer tags, so if a customer is logged in, you can display contextualized information.

{% if shop.customer_accounts_enabled and customer and customer.tags contains 'Wholesale' %}
  <p>Use the discount code WHOLESALE at checkout. That'll give you 40% off. Cheers!</p>
{% endif %}