1
votes

I can't figure out what's going wrong here, I have several other pieces of code with the same structure yet they don't return this error.

Here is the liquid code within the HTML file (item.html) that I'm having trouble with:

{% assign item-collection = site.item-collection | sort: 'date' | reverse %}
{% for item in item-collection %}
  {% if item.featured == true limit: 3 %}
    <div class="item">
    </div>
  {% endif %}
{% endfor %}

Here's an 'item' (item.md) being processed;

---
layout: item
date: 2017-06-08 00:00:00
title: item 1
featured: true
tags:
---

Here's the error returned by Terminal:

Regenerating: 1 file(s) changed at 2017-06-28 22:41:16     Liquid Warning: Liquid syntax error (line 30): Expected end_of_string but found id in "item.featured == true limit: 3" in /_layouts/item.html ...done in 1.337976 seconds.

If I leave the date empty this error doesn't occur, but as soon as something is entered this error stops the site from being built.

The error also disappears if i remove the 'limit: 3' from the liquid code, but I need this limit in place.

Any ideas on what I'm doing wrong? Thanks in advance!

1

1 Answers

2
votes

limit is a for tag parameter. It exits the for loop at a specific index.

Using it after an if tag doesn't mean anything and it is confusing Jekyll when processing it.

Move the limit tag to the for loop line and it should work iterating only the first three items.

{% for item in item-collection limit: 3 %}
 {% if item.featured  %}

update based in comments

  • filter items by featured tag

    {% assign item-collection = site.item-collection | where_exp:"item","item.featured == true" %}
    
  • sort the result by date

    {% assign item-collection = item-collection | sort: 'date' | reverse %}
    
  • Limit list just to 3 featured post

    <ul>
    {% for item in item-collection limit:3 %}
    <li>{{item.date}} - {{item.title}}</li>
    {% endfor %}
    </ul>
    

Summarizing:

{% assign item-collection = site.item-collection | where_exp:"item","item.featured == true" %}

{% assign item-collection = item-collection | sort: 'date' | reverse %}

<ul>
{% for item in item-collection limit:3 %}
<li>{{item.date}} - {{item.title}}</li>
{% endfor %}
</ul>