Search code examples
for-loopincludejekyllliquid

How to pass a limit parameter to an include using Jekyll's Liquid


I have a collection of projects on my site that I iterate over using {% for project in site.projects %} and store in an include called projects-list.html.

I would like to include the latest project from this collection on the homepage as a ‘featured’ item – is it possible to include the projects-list but pass in a limit:1 parameter so that only the first project is shown? Based on the Jekyll docs found here, I have tried passing the parameter to the include like this:

{% for project in site.projects limit:{{ include.limit }} %}

and refercing the include like this:

{% include projects-list.html limit=1 %}

but this does not appear to work. Is this a syntax error or am I missing something?


Solution

  • You could try the first

    {% assign projectFeatured = site.projects | first %}
    
    {% for projects in site.projects %} 
      {% include projects-list.html %} 
    {% endfor %}
    
    {% for projects in projectFeatured %}
     {% include projects-list.html %}
    {% endfor %}
    

    Though I encourage you to add a featured: True in your post and do something like that:

    {% if post.featured == true %}
    
     {% include post.html %}
    
     {% endif %}
    

    https://shopify.github.io/liquid/filters/first/