在液体中显示数据
display data in liquid
我希望在 jekyll 生成的网站上显示来自 csv 文件的信息。我需要在 csv 文件中搜索适当的类别,然后在页面上显示其中的四个。过滤到所选类别没有问题,但我很难将输出限制为四个。
有没有办法对 if 语句应用限制?或者还有其他方法可以写这个吗?我对 Liquid 不是那么精通,所以我极有可能错过了一个明显的解决方案。
使所有适用数据显示在屏幕上的基本代码:
{% for study in site.data.studies %}
{% if study.category contains "foo" %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>Attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% else %}
{% continue %}
{% endif %}
{% endfor %}
我也试过 unless
和 tablerow
,但都没有用。我至少在正确的轨道上吗?我怎样才能限制这个 forloop 停止在四个项目上?
谢谢!
理想情况下,应该在渲染之前过滤数据,但是您也可以创建一个 variable in liquid 来保存渲染的内容数量
{% assign rendered = 0 %}
{% for study in site.data.studies %}
{% if study.category contains "foo" %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% assign rendered = rendered | plus: 1 %}
{% if rendered == 4 %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
正如我所说,理想的解决方案是创建自己的过滤器来完成所有工作(按类别过滤并限制结果数量)
{% assign filtered = site.data.studies | my_custom_filter %}
{% for study in filtered %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% endfor %}
假设你的 category
是一个字符串,而不是一个数组,你可以这样做:
{% assign selected = site.data.studies | where: 'category','foo' %}
{% for study in selected limit:4 %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>Attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% endfor %}
如果你的 category
是一个像 "foo, bar, baz"
这样的字符串或者字符串数组,你可以像这样使用 jekyll 3.2 where_exp
过滤器:
{% assign selected = site.data.studies | where_exp:"item", "item.category contains 'foo'" %}
我希望在 jekyll 生成的网站上显示来自 csv 文件的信息。我需要在 csv 文件中搜索适当的类别,然后在页面上显示其中的四个。过滤到所选类别没有问题,但我很难将输出限制为四个。
有没有办法对 if 语句应用限制?或者还有其他方法可以写这个吗?我对 Liquid 不是那么精通,所以我极有可能错过了一个明显的解决方案。
使所有适用数据显示在屏幕上的基本代码:
{% for study in site.data.studies %}
{% if study.category contains "foo" %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>Attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% else %}
{% continue %}
{% endif %}
{% endfor %}
我也试过 unless
和 tablerow
,但都没有用。我至少在正确的轨道上吗?我怎样才能限制这个 forloop 停止在四个项目上?
谢谢!
理想情况下,应该在渲染之前过滤数据,但是您也可以创建一个 variable in liquid 来保存渲染的内容数量
{% assign rendered = 0 %}
{% for study in site.data.studies %}
{% if study.category contains "foo" %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% assign rendered = rendered | plus: 1 %}
{% if rendered == 4 %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}
正如我所说,理想的解决方案是创建自己的过滤器来完成所有工作(按类别过滤并限制结果数量)
{% assign filtered = site.data.studies | my_custom_filter %}
{% for study in filtered %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% endfor %}
假设你的 category
是一个字符串,而不是一个数组,你可以这样做:
{% assign selected = site.data.studies | where: 'category','foo' %}
{% for study in selected limit:4 %}
<div class="col-sm-3">
<h3>{{ study.title }}</h3>
<div class="list-of-attributes">
<h6>Attributes: </h6>
{{ study.attributes }}
</div>
</div>
{% endfor %}
如果你的 category
是一个像 "foo, bar, baz"
这样的字符串或者字符串数组,你可以像这样使用 jekyll 3.2 where_exp
过滤器:
{% assign selected = site.data.studies | where_exp:"item", "item.category contains 'foo'" %}