Skip to content

ifchanged ​

Outputs its content only when the content is different from the last time the tag rendered. Use it inside a for loop to show a value once for each group of items.

liquid
{% ifchanged %}
  block_to_output
{% endifchanged %}
  • block_to_output: Rendered expression when it is different from the previous one.

Example:

Data
json
{
  "products": [
    { "title": "Hat", "type": "Caps" },
    { "title": "Cap", "type": "Caps" },
    { "title": "Tee", "type": "Shirts" }
  ]
}
liquid
{% for product in products -%}
  {% ifchanged %}<h3>{{ product.type }}</h3>{% endifchanged -%}
  <p>{{ product.title }}</p>
{% endfor %}

Output

html
<h3>Caps</h3><p>Hat</p>
<p>Cap</p>
<h3>Shirts</h3><p>Tee</p>