Liquid Filter Reference for compact - v0.9.5

Filter Name compact
Group Arrays
Version 0.9.5

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.9.5

 

 
Back to Documentation