0

我正在尝试设置 Jekyll,以便在侧边栏中显示帖子列表中第一个帖子的引用,但我不知道该怎么做。我quote在每个帖子的 Markdown 中将引用文本定义为 YML Front Matter 中的变量。

这是我的default.html的相关摘录:

<div id="content">
  {{ content }}
</div>    
<div id="sidebar">
  <blockquote>{{ page.quote }}</blockquote>
</div>    

这是我的index.html

---
layout: default
quote: ** Can a variable referencing the first post go here? **
---
{% for post in site.posts limit:10 %}
  <h2>{{ post.title }}</h2>
  <div class="post">
    {{ post.content }}
  </div>
{% endfor %}
4

2 回答 2

3
{% for post in site.posts limit:10 %}
  {% if forloop.index0 == 0 %}
    {% assign quote = post.quote %}
  {% endif %}

  <h2>{{ post.title }}</h2>
  <div class="post">
    {{ post.content }}
  </div>
{% endfor %}

default.html中

<div id="content">
  {{ content }}
</div>    
<div id="sidebar">
  <blockquote>{{ quote }}</blockquote>
</div> 

我不认为你可以在 YML 问题中存储引用,但这应该得到你想要的。

于 2011-05-28T17:00:46.953 回答
2

经过大量实验,我能够使用default.html中的这个 Liquid 片段来解决问题:

<div id="sidebar">
  <blockquote>
  {% if page.quote %}
    {{ page.quote }}
  {% else %}
    {{ site.posts.first.quote }}
  {% endif %}
  </blockquote>
</div>
于 2011-05-29T11:05:28.403 回答