1
votes

To set the scene, I have author information stored in directory.json, inside my Jekyll _data folder.

"name": "Brandon Bellew",
    "bio": "Brandon Bellew is a great writer.",
    "email": "[email protected]",
    "specialties": "introspection, ventilation systems, memorization, post-prandial revelry",
    "photo": "http://www.tpgweb2.net/headshots/thumbs/brandon-b-thumb-2.jpg"

I also have article information stored in catalogue.json, also inside my Jekyll _data folder

    "name": "Beginner's Guide to Google Analytics",
    "author": "Brandon Bellew",
    "date": "May 18 2016",
    "description": "The Google Analytics (GA) dashboard is quite intuitive, and it's easy to collect basic information (number of users on the site, average time spent on site, etc.) without having a deep background in analytics. But what about specifics?",
    "category": "Analytics"

I'm trying to cycle through all the articles in my catalogue.json file and display that article info, but pull the photo from the correct author in my directory.json file.

In essence, what I'm trying get it to do is: if the "author" of an article in catalogue matches the "name" of an author in a directory item, display the photo associated with that author. That way I don't have to store the author's photo in a separate item for every article they write.

Here's my failed attempt at pulling the photo from the correct author in the directory:

<h4>Articles</h4>
  {% for item in site.data.catalogue %} 
        {% for item in site.data.directory %}
        {% if item.name == page.author %}
        <img src="{{ item.photo }}" >
        {% endif %}
        {% endfor %}
  {% endfor %}

Is this possible using Jekyll or is there a different way I need to structure it?

2

2 Answers

1
votes

I believe removing the duplicate item reference would fix your current code:

{% for catalogue in site.data.catalogue %} 
  {% for item in site.data.directory %}
    {% if item.name == catalogue.author %}
      <img src="{{ item.photo }}" >
    {% endif %}
  {% endfor %}
{% endfor %}

You could structure your author directory so you can access the additional information without having to loop over every author.

authors.json:

{
    "Fred Flintstone": {
      "bio": "Doesn't wear shoes"
      "image": "/images/fred.png"
    }
}

Then you could access the image like so (you could improve this to check if the author exists):

{% for catalogue in site.data.catalogue %} 
  <img src="{{ site.data.authors[catalogue.author].photo }}" >
{% endfor %}
0
votes

You can use where filter like this :

{% for article in site.data.catalogue %} 
  {% assign author = site.data.directory | where: 'name', article.author | first %}
  {% if author %}<img src="{{ author.photo }}" >{% endif %}
  {% endfor %}
{% endfor %}