首页 文章

比较来自不同Jekyll_data文件的数据

提问于
浏览
1

要设置场景,我的作者信息存储在我的Jekyll _data文件夹中的directory.json中 .

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

我还有文章信息存储在catalogue.json中,也在我的Jekyll _data文件夹中

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

我正在尝试遍历我的catalogue.json文件中的所有文章并显示该文章信息,但是从我的directory.json文件中的正确作者中提取照片 .

本质上,我正在尝试让它做的是:如果目录中文章的“作者”与目录项中作者的“名称”匹配,则显示与该作者关联的照片 . 这样我就不必将作者的照片存储在他们编写的每篇文章的单独项目中 .

这是我尝试从目录中的正确作者中提取照片失败的尝试:

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

这可能是使用Jekyll还是我需要一种不同的方式来构建它?

2 回答

  • 0

    我相信删除重复的 item 引用会修复您当前的代码:

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

    您可以构建作者目录,这样您就可以访问其他信息而无需遍历每个作者 .

    authors.json

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

    然后你可以像这样访问图像(你可以改进这个来检查作者是否存在):

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

    您可以像这样使用 where 过滤器:

    {% 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 %}
    

相关问题