【问题标题】:How can I loop from a data file to display content?如何从数据文件循环显示内容?
【发布时间】:2019-09-08 12:29:15
【问题描述】:

如果我指定{% 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循环。

【问题讨论】:

  • 你在乎钥匙吗? (本例中为"attorney1")因为 for 循环可能会遍历键/值对。你可以试试for member in site.data.members.values 看看是否可行?
  • @3limin4t0r 此数据文件中还有其他键。刚卡住!它可以调用特定的键。只是不在 for 循环中。
  • @3limin4t0r 如果我将数据文件格式切换为 - author: attorney1 并在下面有 2 个空格,则循环有效并且作者页面停止为 {% assign member = site.data.members[page.author] %} 工作
  • @3limin4t0r {% assign author = site.data.members | where: "author", "{{page.author}}" %} 在作者页面上也不起作用。
  • 为我的问题投票?

标签: ruby jekyll liquid jekyll-theme


【解决方案1】:

要成功迭代给定列表,您只需要一个结构合理的数据。

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


调查

要确认,您可以先简单地“检查”数据。将以下 sn-p 插入到您的模板中以获取数据的 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 过滤器和firstlast 过滤器:

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

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

【讨论】:

  • 我读过 jekyllrb.com/docs/datafiles 我有一个 YML 文件,你的示例和文档中的示例。循环有效。但是,{% assign member = site.data.members[page.author] 即使在定义要输出哪个律师的frontmatter 中也不起作用。
  • 再次,使用检查过滤器“检查”数据对象。 {{ site.data.members | inspect }}{{ page.author | inspect }} 应该会给你必要的见解。
  • --- layout: attorney author: attorney2 --- {% assign member = site.data.members[page.author] %} &lt;!-- Main --&gt; &lt;article id="main"&gt; &lt;header class="special container"&gt; &lt;span class="icon fas fa-user-circle"&gt;&lt;/span&gt; &lt;h2&gt;About {{ member.name }}&lt;/h2&gt; {{ member.web | markdownify }} {{ page.author | inspect }}{{ site.data.members | inspect }} 将律师 2 显示为作者,并显示数据文件的 jsonify [{~~}] 值。
  • {{ site.data.members[page.author] | inspect | jsonify }} {{ page.author | inspect }} 在 /lawyers/attorney2.html 上产生 "nil" "attorney2"
  • 我已经使用解决方案编辑了答案,以根据给定的密钥提取成员。基本上,您试图用一个数据做两件不同的事情:迭代并呈现每个成员选择特定成员并呈现它。所以有两种不同类型的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 2013-03-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
相关资源
最近更新 更多