显示Jekyll _data文件中的数据



我有一个文件,authors。在我的Jekyll _data文件夹中。它是这样设置的:

short_name:
  name: First Lastname
  email: e@mail.com
  twitter: twitterID
  image: image.jpg
  bio: 'Bio goes here.'

我想在这个文件中为每个作者创建一个列表页。这是我尝试过的:

<ul>
{% for author in site.data.authors %}
  <li>{{ author.name }}</li>
{% endfor %}
</ul>

返回的是与文件中作者数量相匹配的空列表标记数。

我想知道是否有任何方法可以显示每个作者中包含的数据,而无需诉诸YAML文件中的以下内容(因为它会破坏网站上的其他代码):

-author: short_name
  name: First Lastname
  email: e@mail.com
  twitter: twitterID
  image: image.jpg
  bio: 'Bio goes here.'

这可能吗?

或者,当我添加'author'键时被破坏的代码如下:

模板中:

{% assign author = site.data.authors[post.author] %}

在插件中:

 entry_name = site.data['authors'][entry.author]

我不确定这是不是你想要的,但我一直在努力解决一个类似的问题。

{% for member in site.data.authors %}
    {% if member.short_name == post.author %}
        {% assign author = member %}
    {% endif %}
{% endfor %}

为此工作,我需要在我的数据中包含一个short_name项。见下文:

- author: short_name
  name: First Lastname
  short_name: first_lastname
  email: e@mail.com
  twitter: twitterID
  image: image.jpg
  bio: 'Bio goes here.'

现在我们可以在模板中使用{{ author.name }}等。

我实际上已经写了一篇关于这个主题的博客文章。在_data文件中,您只需要为想要显示的每组作者属性创建一个新条目。

所以,如果你改变了作者。

- name: John Doe
  email: john.doe@example.com
  twitter: @johndoe
- name: Mary Doe
  email: mary.doe@example.com
  twitter: @notjohndoe

然后你可以在author对象上引用你要查找的属性:

{% for author in site.data.authors %
  {% author.name %}
  {% author.email %}
  ... etc ...
{% endfor %}

希望对你有帮助。

我也有同样的问题。这是最好的解决方案。

这是我的布局文件的摘录:

---
layout: sidebar-container
author: paul
includeHeader: blog-image.html
---
{% assign author = site.data.people.[page.author] %}

<article class="container">
    <header>
        <h1 class="title">{{ page.title }}</h1>
        {% if page.subtitle %}<h2 class="subtitle">{{ page.subtitle }}</h2>{% endif %}
        <div class="meta">
            By <address><a rel="author" href="{{ author.link }}" title="{{ author.name }}" target="_blank">{{ author.name }}</a></address> &mdash;
            <time pubdate datetime="{{ page.date | date: "%Y-%d-%B" }}" title="{{ page.date | date: "%B %d, %Y" }}">{{ page.date | date: "%B %d, %Y" }}</time>
        </div>
    </header>

这是我的数据文件名为'people.yml'

dave:
    id: dave
    name: David Smiths
    link: /bitcoin-expert/
    twitter: DavidSilvaSmith
    image: http://www.gravatar.com/avatar/7bbd083ea04a3c791e878da24c08b987.png
    bio: David is the CEO of bitcoin bulls.
paul:
    id: paul
    name: Paul
    link: http://www.paulsmith.com
    twitter: PaulSmith
    image: http://www.cs.mcgill.ca/~kry/kry-acm1.png
    bio: Paul Smith rocks

最新更新