【问题标题】:For Loops and Jekyll: Selecting Specific Indices in a loop?对于循环和 Jekyll:在循环中选择特定索引?
【发布时间】:2018-02-24 00:32:54
【问题描述】:

假设你有一个数据库:

品牌 |型号 |序列号 |位置 | IP 地址 |服务日期 |等等……

为了创建列标题,我确定循环中的第一个标题,然后使用 field[0] 为集合中的每一列运行另一个 for 循环。然后,我从左到右对数据和 field[1] 数据运行另一个 for 循环。

这很好地构建了表格。如何遍历字段 [1] for 循环并有选择地选择每一列?假设有一页我想显示品牌 |型号 |序列号,但在另一页上我只想包含品牌 |型号 | IP 地址。

到目前为止,我设法做到这一点的唯一方法是在 forloop.index 字段内放置一个 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>

【问题讨论】:

    标签: for-loop jekyll


    【解决方案1】:

    您可以通过索引来识别一列:

    Brand | Model | Serial No | Location | IP Address
    1       2       3           4          5
    

    然后您可以根据一个简单的数组选择打印的列。在本例中,它存储在页面前端,但也可以放在前端_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>
    

    编辑

    您还可以使用来自页面逻辑的选择数组,例如 {% assign displayCol = "1,2,4" | split: "," %}(从字符串创建数组,这是在页面代码中创建数组的唯一方法),引用为 displayCol 而不是 page.displayCol

    问题在于它创建了一个字符串数组:{% assign displayCol = "1,2,4" | split: "," %} =&gt; ["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>
    

    【讨论】:

    • 不使用front matter,是否可以在包含[1, 2, 4] 的逻辑内部定义一个数组,然后使用该变量,还是仅限于front matter?
    • 是的,这会起作用,因为我可以根据页面名称的直接结果来分配 displayCol。因此,如果 pages 文件夹中的页面名为“equipment_list_sound.md”并且它包含逻辑页面_includes/equipment_list.html,那么在逻辑中我可以从 {{page.name}} 中删除“equipment_list_”,只留下“声音” ,然后运行一个条件,如果 X = 声音,则 displayCol = Y,否则如果 X = 灯光,则 displayCol = Z。
    • 评论不是就不属于它的元素提出其他问题的地方。记下答案,然后在需要时提出新问题。
    • 这不是问题。
    • 好的,我明白你的意思了。
    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多