1

假设我有一个 MarkDown 文件,它是 Jekyll 静态网站中的一个帖子。它驻留在_posts其中并被命名为2020-05-16-my-post.md。假设其内容由下式给出:

---
title: 'My Post'
date:   2020-05-16
author: My Name
layout: post
class:  news
---
This is the 1st sentence which is should appear both on the index page and the post.
This is the 2nd sentence which should appear only on the index page.
This is the 3rd sentence which should appear only on the post.
This is the 4th sentence which should appear on both.

This is the beginning of the 2nd paragraph...

所以在第 1 段中,我们有 4 个句子。我希望其中一些只出现在索引页(第 2 句)中,其中一些只出现在帖子(第 3 句)中,其余的都出现在两者上。

让我们假设在索引页面中我使用了类似的东西:

<ul>
  {% for post in site.posts %}
    <li>
      <a href="{{ post.url }}">{{ post.title }}</a>
      {{ post.excerpt }}
    </li>
  {% endfor %}
</ul>

因此,在索引页面中,它使用页面第一段中的内容(因为我没有明确定义excerpt标签)。

我以为我可以做这样的事情:

This is the 1st sentence which is should appear both on the index page and the post.
{% if page.layout == "index" %}
This is the 2nd sentence which should appear only on the index page.
{% endif %}
{% if page.layout == "post" %}
This is the 3rd sentence which should appear only on the post.
{% endif %}
This is the 4th sentence which should appear on both.

This is the beginning of the 2nd paragraph...

但似乎 Jekyll 仅在单一上下文中呈现页面(其显式 YAML)。

有没有办法这样做?即不同的页面会绘制不同的渲染内容?

备注
我知道使用excerpts和的选项excerpt_separator。然而,它们在我的用例中并不令人满意。

4

0 回答 0