首页 文章

显示来自Jekyll _data文件的数据

提问于
浏览
2

所以我在Jekyll _data文件夹中有一个文件authors.yml . 它的设置如下:

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

这可能吗?

或者,添加“作者”键时被破坏的代码如下 .

在模板中:

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

在一个插件中:

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

3 回答

  • 1

    我不确定这是否是你所追求的,但我一直在努力解决类似的问题 .

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

  • 0

    关于这个问题,我实际上是written a blog post . 在_data文件中,您只需为要显示的每组作者属性创建一个新条目 .

    因此,如果您将authors.yml文件更改为如下所示:

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

    然后,您可以在作者对象上引用您要查找的属性:

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

    希望有所帮助 .

  • 1

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

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

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

相关问题