Jekyll自动处理行 [英] Jekyll automatically processing rows
问题描述
我想从site.pages
中过滤出所有具有给定YAML前题类型'project'
的页面.
I want to filter out from site.pages
all pages with a given YAML frontmatter type 'project'
.
为此,我已经这样做:
{% sorted_for page in site.pages sort_by:title %}
{% if page.type == 'project' %}
do something
{% endif %}
{% endfor %}
这使用了 sorted_for 插件.但是,我现在要做的是将这些排列成四行的css行.
This uses the sorted_for plugin. What I want to do now, however is to arrange these in css rows of four.
<div class="row">
<div class="span3">{{ page1.title }}</div>
<div class="span3">{{ page2.title }}</div>
<div class="span3">{{ page3.title }}</div>
<div class="span3">{{ pagen.title }}</div>
</div>
...
以此类推,直到行用完.如何获得执行此操作所需的信息?不幸的是,for循环迭代器号将是错误的,因为它还会在非项目页面上打勾.有没有一种干净的方法来实现这一点?
And so forth, until the rows run out. How can I access the information I need to do this? Unfortunately the for loop iterator number will be wrong, as it will also tick through the non-project pages. Is there a clean way to implement this?
推荐答案
注意:
我无法在计算机上使用sorted_for
插件,因此我改用常规的for
测试了我的解决方案.
Note:
I couldn't get the sorted_for
plugin to work in my machine, so I tested my solution with a regular for
instead.
您不能使用 forloop.index
,因为您正在过滤掉某些页面,则需要使用 assign
.
As you can't use forloop.index
because you're filtering out some pages, you need to count the loops by yourself, by writing to a variable with assign
.
以下代码将使用正确的循环迭代器(仅计算实际列出的页面)列出您的页面:
The following code will just list your pages with a correct loop iterator (by counting just the pages that are actually listed):
{% assign loopindex = 0 %}
{% for page in site.pages %}
{% if page.type == 'project' %}
{% assign loopindex = loopindex | plus: 1 %}
<div class="span3">{{ loopindex }} {{ page.title }}</div>
{% endif %}
{% endfor %}
步骤2
您需要在第一行中显示<div class="row">
,并在第四行中显示</div>
.
Step 2
You need to display <div class="row">
with every first row and </div>
with every fourth row.
要查找第一行和第四行,可以使用modulo
:
To find the first and fourth rows, you can use modulo
:
{% assign loopindex = 0 %}
{% for page in site.pages %}
{% if page.type == 'project' %}
{% assign loopindex = loopindex | plus: 1 %}
{% assign rowfinder = loopindex | modulo: 4 %}
<div class="span3">{{ loopindex }} {{ rowfinder }} {{ page.title }}</div>
{% endif %}
{% endfor %}
rowfinder
将始终重复序列 1、2、3、0 .
因此,当rowfinder
为1
时显示<div class="row">
,而当rowfinder
为0
时显示</div>
:
So you display <div class="row">
when rowfinder
is 1
, and </div>
when rowfinder
is 0
:
{% assign loopindex = 0 %}
{% for page in site.pages %}
{% if page.type == 'project' %}
{% assign loopindex = loopindex | plus: 1 %}
{% assign rowfinder = loopindex | modulo: 4 %}
{% if rowfinder == 1 %}
<div class="row">
<div class="span3">{{ page.title }}</div>
{% elsif rowfinder == 0 %}
<div class="span3">{{ page.title }}</div>
</div>
{% else %}
<div class="span3">{{ page.title }}</div>
{% endif %}
{% endif %}
{% endfor %}
步骤4:
现在只剩下一件事了:当页数不是4的倍数时,末尾会缺少</div>
.
如果页数是的4的倍数,则rowfinder
的最后一个值将是0
.
因此,当rowfinder
的值不是0
时,我们只需要显示</div>
.
因此,只需将其放在for循环之后:
When the number of pages is a multiple of 4, the last value of rowfinder
will be 0
.
So we just need to display the </div>
when the value of rowfinder
is anything else but 0
.
So just put this after the for loop:
{% if rowfinder != 0 %}
</div>
{% endif %}
...就是这样!
这篇关于Jekyll自动处理行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!