1

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

==成员.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循环。

4

1 回答 1

2

要成功地遍历给定的列表,您所需要的只是一个结构合理的数据。

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


调查

要确认,您可以先简单地“检查”数据。将以下代码段插入到您的模板中以获取数据的 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"

选择

要根据给定键选择特定数据集,您可以将数据和键传递给where过滤器和firstorlast过滤器:

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

使用上述方法,首先在member.authorequals处生成另一个成员数组,page.author然后通过first过滤器提取第一个条目。

于 2019-09-08T16:47:48.743 回答