如何使用 Jekyll 的 Liquid 将限制参数传递给包含
How to pass a limit parameter to an include using Jekyll's Liquid
我在我的网站上有一个项目集合,我使用 {% for project in site.projects %}
对其进行迭代并存储在名为 projects-list.html
.
的包含中
我想将此系列中的最新项目作为“特色”项目包含在主页上 – 是否可以包含 projects-list
但传入 limit:1
参数,以便仅第一个项目显示?根据此处找到的 Jekyll 文档,我尝试将参数传递给 include,如下所示:
{% for project in site.projects limit:{{ include.limit }} %}
并像这样引用包含:
{% include projects-list.html limit=1 %}
但这似乎不起作用。这是语法错误还是我遗漏了什么?
您可以先尝试
{% assign projectFeatured = site.projects | first %}
{% for projects in site.projects %}
{% include projects-list.html %}
{% endfor %}
{% for projects in projectFeatured %}
{% include projects-list.html %}
{% endfor %}
尽管我鼓励您在 post 中添加一个 featured: True
并执行类似的操作:
{% if post.featured == true %}
{% include post.html %}
{% endif %}
下面是我当前的解决方案(使用 featured: true
)添加到 post frontmatter。这真的很讨厌,但现在似乎可以工作:
<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>
我在我的网站上有一个项目集合,我使用 {% for project in site.projects %}
对其进行迭代并存储在名为 projects-list.html
.
我想将此系列中的最新项目作为“特色”项目包含在主页上 – 是否可以包含 projects-list
但传入 limit:1
参数,以便仅第一个项目显示?根据此处找到的 Jekyll 文档,我尝试将参数传递给 include,如下所示:
{% for project in site.projects limit:{{ include.limit }} %}
并像这样引用包含:
{% include projects-list.html limit=1 %}
但这似乎不起作用。这是语法错误还是我遗漏了什么?
您可以先尝试
{% assign projectFeatured = site.projects | first %}
{% for projects in site.projects %}
{% include projects-list.html %}
{% endfor %}
{% for projects in projectFeatured %}
{% include projects-list.html %}
{% endfor %}
尽管我鼓励您在 post 中添加一个 featured: True
并执行类似的操作:
{% if post.featured == true %}
{% include post.html %}
{% endif %}
下面是我当前的解决方案(使用 featured: true
)添加到 post frontmatter。这真的很讨厌,但现在似乎可以工作:
<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>