如何循环某个字段不为空的所有项目?

时间:2016-02-28 18:16:51

标签: jekyll liquid

我想遍历集合中具有特定字段集(非空)的所有项目。我试过这个:

{% assign papers_with_demos=site.data.papers | where:"demo", not blank %}
{% if papers_with_demos.size > 0 %}
   <h2>demos</h2>
   {% for paper in papers_with_demos %}]
   ... 
   {% endfor %}
{% endif %}

但它不起作用;所有论文都归还。

我的目标是标题&#34;演示&#34;只有在有一个或多个带有演示的论文时才会显示。

1 个答案:

答案 0 :(得分:0)

如果要退回文件,则必须使用数据文件。如果数据文件中某个特定键的值为空,则返回false。所以你可以用forloop检查数据键,如:

{% for paper in site.data.papers %}
  {% if paper.demo %}
  something
  {% endif %}
{% endfor %}

您可以在Jekyll文档的“数据文件”部分找到更多信息。

根据您的回复更新:

{% for item in site.data.items %}
  {% if item.random %}
  {% assign random-val = item.random %}
    {% if random-val %}
    <p>true</p>
    {% endif %}
  {% endif %}
{% endfor %}

如果在items.yml中,你将返回true - random:true,如果为空,则返回true。