如何从数据文件循环显示内容?

How can I loop from a data file to display content?

数据可以显示,如果我指定{% assign member = site.data.members[page.author] %}并在页面的frontmatter中插入author: valuehere。如果我指定 {% for member in site.data.members %} ~~~ {{ member.name }} ~~~ {% endfor %}

它不会循环

== members.yml

attorney1:
  birth_country: "United States of America"
  birth_city: "Paso Robles"
  birth_region: CA
  birth_zip: 93446
  birth_date: "05/1968"
  education: "Southeastern University: B.A. History – 2008, University of Florida Levin College of Law: Juris Doctor – 2001"
  image: attorney1.jpg
  nationality: "United States of America"
  name: "Attorney One Esq"
  first_name: "Attorney"
  last_name: "One"
  honorary: Esquire
  email: email@example.com
  home_country: "United States of America"
  home_city: "Ocala"
  home_region: "FL"
  home_zip: "34482"
  gender: Male
  permalink: "/lawyers/attorney1.html"
  ext: "02"
  practices: "Personal Injury · Insurance Litigation"
  web: "Lawyer One Esq is a past member of the Hillsborough County Bar Association and Young Lawyers Division, the Lakeland Bar Association, and Emerge. Jon was also served on the Board of Directors for Tri-County Human services, which serves Polk, Hardee, and Highlands counties. Lawyer One Esq is currently a member of the Jacksonville Bar Association."

我试过像这样重新格式化数据文件:

- author: attorney1
  name: "Attorney One"
~~~

然后像这样重新编码作者页面:

---
layout: attorney
title: "Attorney One"
crumbtitle: "Attorney One"
permalink: "/lawyers/attorney1.html"
jsontype: lawyer
genre: Law
headline: "Affordable Marion County Legal Representation"
author: attorney1
---
{% assign author = site.data.members | where: "author", "{{page.author}}" %}
<!-- Main -->
<article id="main">
  <header class="special container">
    <span class="icon fas fa-user-circle"></span>
    <h2>About {{ author.name }}</h2>
    {{ author.web | markdownify }}
  </header>
  <!-- One -->

目标是能够使用 for 循环并为作者页面提取数据。如果我像这样格式化数据文件:

attorney1:
    name: "Attorney one"

作者页面使用 {% assign author = site.data.members[page.author] %} 并打破 for 循环。

要成功迭代 给定列表,您只需要结构正确的数据。

为了 {% for member in site.data.members %} 正确循环,site.data.members 必须是一个 成员数组 。但是从您发布的信息来看,结果数据似乎是键值对的 Hash(或 dictionary)而不是 Array


调查

要确认,您可以先"inspect"数据。将以下代码段插入您的模板以获得数据的 JSON 表示:

<pre>
{{ site.data.members | inspect }}
</pre>

要成功迭代,生成的 JSON 应该以方括号 ([, ]):

[
  {
    "attorney1": {
      "birth_country": "United States of America",
      "birth_city": "Paso Robles"
    }
  },
  {
    "attorney2": {
      "birth_country": "United States of America",
      "birth_city": "Paso Robles"
    }
  },
]

但是,您的 members.yml 会产生类似于:

{
  "attorney1": {
    "birth_country": "United States of America",
    "birth_city": "Paso Robles"
  },
  "attorney2": {
    "birth_country": "United States of America",
    "birth_city": "Paso Robles"
  }
}


解决方案

单个文件

如果您想将所有律师信息放在一个 YAML 文件中,则结构为:

# _data/members.yml

- attorney1:
    birth_country: "United States of America"
    birth_city: "Paso Robles"
- attorney2:
    birth_country: "United States of America"
    birth_city: "Paso Robles"

单个文件

或者如果您想单独组织个人信息:

# _data/members/attorney1.yml

birth_country: "United States of America"
birth_city: "Paso Robles"
# _data/members/attorney2.yml

birth_country: "United States of America"
birth_city: "Paso Robles"

选择

到 select 基于给定键的特定数据集,您可以将数据和键传递给 where 过滤器和 firstlast 过滤器:

{% assign member = site.data.members | where: 'author', page.author | first %}

根据以上内容,首先生成另一个成员数组,其中 member.author 等于 page.author,然后通过 first 过滤器提取第一个条目。