Let's say I have a MarkDown file which is a post in a Jekyll static web site. It resides in _posts
and it is named 2020-05-16-my-post.md
. Assume its content is given by:
---
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...
So in the 1st paragraph we have 4 sentences. Which I want some of them to appear only in the index page (2nd sentence) some of them only in the post (3rd) and the rest on both.
Let's assume that in the index page I use something like:
<ul>
{% for post in site.posts %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
{{ post.excerpt }}
</li>
{% endfor %}
</ul>
So in the index page it uses what's in the 1st paragraph of the page (As I didn't explicitly define the excerpt
tag).
I thought I could do something like that:
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...
But it seems Jekyll renders the page only in single context (Of its explicit YAML).
Is there a way to do so? Namely different pages will draw different rendering of the content?
Remark
I am aware of the options to use excerpts
and excerpt_separator
. Yet they are not satisfying in my use case.