Search code examples
rubyjekyllliquidjekyll-theme

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


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.


Solution

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