compact - Liquid Filter Reference - 1.0.0

Filter Name compact
Group Arrays
Version 1.0.0

Description

Removes any nil values from an array.

Examples

In this example, categories is an array of category names with some nil values sprinkled in. If we loop over those categories and output them, you can see where the nil values are:
{% for category in categories -%}
- {{ category }}
{% endfor %}
- Geography
- Entertainment
- 
- History
- Art & Literature
- 
- Science & Nature
- Sports & Leisure
When compact is used, it removes the nil values from the array:
{% assign compacted_categories = categories | compact %}

{% for category in compacted_categories | compact -%}
- {{ category }}
{% endfor %}
- Geography
- Entertainment
- History
- Art & Literature
- Science & Nature
- Sports & Leisure

Back to Filter List

Back to Liquid Reference for 1.0.0

 

 
Back to Documentation