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 protected]
  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.

5
  • 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? Commented Sep 8, 2019 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. Commented Sep 8, 2019 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] %} Commented Sep 8, 2019 at 14:02
  • @3limin4t0r {% assign author = site.data.members | where: "author", "{{page.author}}" %} doesn't work, either, on the author page. Commented Sep 8, 2019 at 14:12
  • Vote for my question? Commented Sep 10, 2019 at 11:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I read 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.
Again, use the inspect filter to "check" the data object. {{ site.data.members | inspect }} and {{ page.author | inspect }} should give you the necessary insights.
--- layout: attorney author: attorney2 --- {% assign member = site.data.members[page.author] %} <!-- Main --> <article id="main"> <header class="special container"> <span class="icon fas fa-user-circle"></span> <h2>About {{ member.name }}</h2> {{ member.web | markdownify }} {{ page.author | inspect }}{{ site.data.members | inspect }} shows attorney2 as author and the jsonify [{~~}] values of datafile.
{{ site.data.members[page.author] | inspect | jsonify }} {{ page.author | inspect }} produces "nil" "attorney2" on /lawyers/attorney2.html
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.