【问题标题】:Accessing nested data in Jekyll for loop在 Jekyll for 循环中访问嵌套数据
【发布时间】:2020-02-12 12:57:05
【问题描述】:

我有点难以理解如何使用 Jekyll 访问一些嵌套数据。我希望有人可以帮助我。我正在尝试访问 prototypes.yml 文件中的数据。它用于在仪表板上填充卡片。但是,当我运行循环时,什么都没有返回。我的猜测是我没有正确定位details,但最终我有点不知所措。

prototypes.yml

ios:
  details:
    -
      category: "category"
      title: "title"
      desc: "desc"
      author: "Sean"
      update: "12 Feb 2020"

android:
  details:
    -
      category: "category"
      title: "title"
      desc: "desc"
      author: "Sean"
      update: "12 Feb 2020"

HTML

{% for row in site.data.prototypes %}
  {% for detail in row.details %}
  <a href="{{ detail.permalink }}" class="c-card c-card--{{ detail.category }}">
    <h2>{{ detail.title }}</h2>
    <p>{{ detail.desc }}</p>
    <span>{{ detail.update }}</span>
  </a>
  {% endfor %}
{% endfor %}

【问题讨论】:

    标签: for-loop yaml jekyll


    【解决方案1】:

    row 包含两个值,键(例如ios)及其值。所以你需要

    {% for row in site.data.prototypes %}
      {% for detail in row[1].details %}
      <a href="{{ detail.permalink }}" class="c-card c-card--{{ detail.category }}">
        <h2>{{ detail.title }}</h2>
        <p>{{ detail.desc }}</p>
        <span>{{ detail.update }}</span>
      </a>
      {% endfor %}
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多