1

Data can display if I specify {% assign member = site.data.members[page.author] %} and insert author: valuehere in a page's frontmatter. It will not loop if I specify {% 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."

I've tried reformatting data file like this:

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

Then recode author page like this:

---
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 -->

The goal is to be able to use a for loop and to pull data for an author page. If I format data file like:

attorney1:
    name: "Attorney one"

the author page works with {% assign author = site.data.members[page.author] %} and breaks the for-loop.

  • 1
    Do you care about the keys? (`"attorney1"` in this example) Since the *for*-loop might loop through the key/value pairs. Could you try `for member in site.data.members.values` instead and see if that works? – 3limin4t0r Sep 08 '19 at 13:38
  • @3limin4t0r there are other keys in this data file. Just stuck! It works calling a specific key. Just not in a for loop. – Denver Prophit Jr. Sep 08 '19 at 13:57
  • @3limin4t0r if I switch data file format to `- author: attorney1` with values 2 spaces below, the loop works and the author page stops working for `{% assign member = site.data.members[page.author] %}` – Denver Prophit Jr. Sep 08 '19 at 14:02
  • @3limin4t0r `{% assign author = site.data.members | where: "author", "{{page.author}}" %}` doesn't work, either, on the author page. – Denver Prophit Jr. Sep 08 '19 at 14:12
  • Vote for my question? – Denver Prophit Jr. Sep 10 '19 at 11:06

1 Answers1

3

To successfully iterate through a given list, all you need is a properly structured data.

For {% for member in site.data.members %} to loop properly, site.data.members has to be an array of members. But from the info you have posted, it looks like the resulting data is a Hash (or dictionary) of key-value pairs instead of an Array.


Investigation

To confirm, you may simply "inspect" the data first. Insert the following snippet into your template to get a JSON representation of your data:

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

To iterate successfully, the resulting JSON should begin and end with square brackets ([, ]):

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

But instead, your members.yml would yield something similar to:

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


Solutions

Single file

If you'd like to have all the attorney info in one YAML file, then the structure would be:

# _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"

Individual files

Or if you'd like organize individual info separately:

# _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"

Selection

To select a particular dataset based on a given key, you can pass the data and key to the where filter and the first or last filters:

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

With the above, first another array of members is generated where member.author equals page.author and then the very first entry is extracted via the first filter.

ashmaroli
  • 5,209
  • 2
  • 14
  • 25
  • I read https://jekyllrb.com/docs/datafiles/ I have the single YML file your example and exampled in the docs. The loop works. But, `{% assign member = site.data.members[page.author]` will not work even with the frontmatter defining which lawyer to output. – Denver Prophit Jr. Sep 08 '19 at 18:13
  • 1
    Again, use the inspect filter to "check" the data object. `{{ site.data.members | inspect }}` and `{{ page.author | inspect }}` should give you the necessary insights. – ashmaroli Sep 08 '19 at 18:57
  • ```--- layout: attorney author: attorney2 --- {% assign member = site.data.members[page.author] %}

    About {{ member.name }}

    {{ member.web | markdownify }} {{ page.author | inspect }}{{ site.data.members | inspect }} ``` shows attorney2 as author and the jsonify [{~~}] values of datafile.
    – Denver Prophit Jr. Sep 08 '19 at 19:31
  • `{{ site.data.members[page.author] | inspect | jsonify }} {{ page.author | inspect }}` produces `"nil" "attorney2"` on /lawyers/attorney2.html – Denver Prophit Jr. Sep 08 '19 at 19:41
  • 1
    I've edited the answer with the solution to extract a member based on given key. Basically, you're trying to do two different things with one data: *iterate and render each member* and *select a particular member and render that*. So there are two different types of solutions – ashmaroli Sep 09 '19 at 12:22
  • Please +1 my question? – Denver Prophit Jr. Sep 10 '19 at 21:32