How to use an include with limit parameter

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 using 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?

why pass the limit in? why not hard code it in the include file itself? will you want to do other limits besides 1?

ah - cause you want to use it in 2 places, one with everything and one with limit:1?

My guess is you can’t process the for loop with the liquid include parameter at the same time? Maybe do what the docs say in their example with assign to get the limit into a liquid variable before you do the for loop? just guessing, not sure at all but worth a shot.

You may also be able to access include.limit without the curlies around it? like:

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

pretty sure you don’t need the curlies around include.limit since you are already inside the code block.

1 Like

yes, exactly right – i want to include it in more than one place with a limit parameter on one but not on the other. your suggestion worked! thank you. it only needs to be wrapped in curly braces if it’s a captured variable – i was a little confused by the documentation on the jekyll site. thanks for your help.