compact - Liquid Filter Reference - 0.11.4

Filter Name compact
Group Arrays
Version 0.11.4

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 0.11.4

 

 
Back to Documentation