I have a simple Blog site where the front page index is a list of posts, non truncated and rendered exactly like the individual pages.
I have the index page setup and working:
---
layout: default
---
<div>
{% for post in site.posts %}
{% include post/post.html %}
{% endfor %}
</div>
Where post/post.html
contains the post layout using the post
variable like so:
<article>
<header>
{% include post/title.html %}
{% include post/metadata.html %}
</header>
<div class="entry-content" itemprop="text">
{{ post.content }}
</div>
</article>
Now, on a specific post page, I want to reuse this include layout so that I can keep my code DRY, so I have the posts use the post.html
layout (different from posts/post.html
above):
---
layout: default
comments: true
---
{% include post/post.html %}
but the problem is that the include file expects a post
variable to exist such post.content
is how you access the content.
I have tried:
{% assign post = page %}
and that seems to be the right to pass a variable in, but page is not the right one as it renders as a markdown string instead of the html on the page.
So, how can I pass self
-- or whatever is needed -- so that the include file does not need to be altered for the index page or the post page, thus sharing the same code?
While @David Jacquel's answer work, I found it to be unclean and a little verbose, though it did get me on the right track.
_includes/post/post.html
swaps the {{ post.content }}
for {{ body }}
<article>
<header>
{% include post/title.html %}
{% include post/metadata.html %}
</header>
<div class="entry-content" itemprop="text">
{{ body }}
</div>
</article>
_layouts/index.html
now uses
{% assign body = post.content %}
{% include post/post.html %}
_layouts/post.html
uses:
{% assign post = page %}
{% assign body = content %}
{% include post/post.html %}
Simple.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments