简体   繁体   中英

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?

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/

Below is my current solution (with featured: true ) added to post frontmatter. It's really nasty but appears to work for now:

<div class="project-list">
  {% if page.layout == "home" %}
    {% assign projects = site.projects | where: "featured", "true" %}
    {% for project in projects %}
      {% include project/project.html %}
    {% endfor %}
  {% else %}
    {% assign projects = site.projects %}
    {% for project in projects %}
      {% include project/project.html %}
    {% endfor %}
  {% endif %}
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM