For 循环和 Jekyll:在循环中选择特定索引?
For Loops and Jekyll: Selecting Specific Indices in a loop?
假设您有一个数据库:
品牌 |型号 |序列号 |地点 | IP 地址 |服务日期 |等等...
为了创建列 headers,我在循环中确定了第一个,然后 运行 使用字段 [0] 为集合中的每一列确定了另一个 for 循环。然后,我 运行 从左到右对数据和字段 [1] 数据进行另一个 for 循环。
这很好地构建了 table。我如何运行通过字段[1] for循环并有选择地选择每一列?假设我想在一页上展示 Brand |型号 |序列号,但在另一页上我只想包含品牌 |型号 | IP 地址。
直到现在,我设法做到这一点的唯一方法是在字段内放置一个 forloop.index 条件,以便循环查找 {% if forloop.index == 1 and forloop.index == 3 %}
作为示例等。这不会'看起来很有效率。
<table>
{% for item in site.data.equipment %}
{% if forloop.first == true %}
<tr>
{% for field in item %}
<th>{{ field[0] }}</th>
{% endfor %}
</tr>
{% endif %}
{% for item in site.data.equipment %}
<tr>
<td>{{ field[1] }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
您可以通过索引来识别列:
Brand | Model | Serial No | Location | IP Address
1 2 3 4 5
然后您可以 select 打印基于简单数组的列。在本例中,它存储在页面前面,但也可以放在前面 _config.yml.
---
# page front matter
# ....
displayCol: [1,2,4]
---
{% assign equipment = site.data.equipment %}
<table>
{% for item in equipment %}
{% if forloop.first == true %}
<tr>
{% for field in item %}
{% if page.displayCol contains forloop.index %}
<th>{{ field[0] }}</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
<tr>
{% for field in item %}
{% if page.displayCol contains forloop.index %}
<td>{{ field[1] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
编辑 :
您还可以使用来自页面逻辑的 selection 数组,例如 {% assign displayCol = "1,2,4" | split: "," %}
(从字符串创建数组,这是在页面代码中创建数组的唯一方法),引用为displayCol
而不是 page.displayCol
.
问题是它创建了一个字符串数组:{% assign displayCol = "1,2,4" | split: "," %} => ["1", "2", "4"]
。并且无法测试 forloop.index(integer) 在字符串数组中的存在。
解决方案是将 forloop.index 转换为具有 {% assign indexToStr = forloop.index | append:"" %}
的字符串
结果代码将是:
{% assign equipment = site.data.equipment %}
{% comment %}Here is the setup for displayed columns{% endcomment %}
{% assign displayCol = "1,2,4" | split: "," %}
<table>
{% for item in equipment %}
{% if forloop.first == true %}
<tr>
{% for field in item %}
{% assign indexToStr = forloop.index | append: "" %}
{% if displayCol contains indexToStr %}
<th>{{ field[0] }}</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
<tr>
{% for field in item %}
{% assign indexToStr = forloop.index | append: "" %}
{% if displayCol contains indexToStr %}
<td>{{ field[1] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
假设您有一个数据库:
品牌 |型号 |序列号 |地点 | IP 地址 |服务日期 |等等...
为了创建列 headers,我在循环中确定了第一个,然后 运行 使用字段 [0] 为集合中的每一列确定了另一个 for 循环。然后,我 运行 从左到右对数据和字段 [1] 数据进行另一个 for 循环。
这很好地构建了 table。我如何运行通过字段[1] for循环并有选择地选择每一列?假设我想在一页上展示 Brand |型号 |序列号,但在另一页上我只想包含品牌 |型号 | IP 地址。
直到现在,我设法做到这一点的唯一方法是在字段内放置一个 forloop.index 条件,以便循环查找 {% if forloop.index == 1 and forloop.index == 3 %}
作为示例等。这不会'看起来很有效率。
<table>
{% for item in site.data.equipment %}
{% if forloop.first == true %}
<tr>
{% for field in item %}
<th>{{ field[0] }}</th>
{% endfor %}
</tr>
{% endif %}
{% for item in site.data.equipment %}
<tr>
<td>{{ field[1] }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
您可以通过索引来识别列:
Brand | Model | Serial No | Location | IP Address
1 2 3 4 5
然后您可以 select 打印基于简单数组的列。在本例中,它存储在页面前面,但也可以放在前面 _config.yml.
---
# page front matter
# ....
displayCol: [1,2,4]
---
{% assign equipment = site.data.equipment %}
<table>
{% for item in equipment %}
{% if forloop.first == true %}
<tr>
{% for field in item %}
{% if page.displayCol contains forloop.index %}
<th>{{ field[0] }}</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
<tr>
{% for field in item %}
{% if page.displayCol contains forloop.index %}
<td>{{ field[1] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
编辑 :
您还可以使用来自页面逻辑的 selection 数组,例如 {% assign displayCol = "1,2,4" | split: "," %}
(从字符串创建数组,这是在页面代码中创建数组的唯一方法),引用为displayCol
而不是 page.displayCol
.
问题是它创建了一个字符串数组:{% assign displayCol = "1,2,4" | split: "," %} => ["1", "2", "4"]
。并且无法测试 forloop.index(integer) 在字符串数组中的存在。
解决方案是将 forloop.index 转换为具有 {% assign indexToStr = forloop.index | append:"" %}
结果代码将是:
{% assign equipment = site.data.equipment %}
{% comment %}Here is the setup for displayed columns{% endcomment %}
{% assign displayCol = "1,2,4" | split: "," %}
<table>
{% for item in equipment %}
{% if forloop.first == true %}
<tr>
{% for field in item %}
{% assign indexToStr = forloop.index | append: "" %}
{% if displayCol contains indexToStr %}
<th>{{ field[0] }}</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
<tr>
{% for field in item %}
{% assign indexToStr = forloop.index | append: "" %}
{% if displayCol contains indexToStr %}
<td>{{ field[1] }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>