Greg Thompson
Greg Thompson

Reputation: 894

Jekyll content blocks in posts

so I've searched high and low for a solution but can't quite find anything that works in a clean way. What I'd like to do is define a content block and use that content block not only in the post itself but in a loop of posts as well. Consider it something like custom content types for posts.

I'd like to do something like this...

in my test_post.md:

---
layout: post
title: Test Post
---
This is the random post content

{% capture test %} This is a test capture {% endcapture %}

in my post.html I'd like to do something like:

---
layout: default
---
<article class="post">
{{ content }}
<p>{{test}}</p>
</article>

I know that I can do this in the front matter but this seems dirty to me. I also found a plugin That almost does what I need but it doesn't allow any access to these content blocks outside of the post page, meaning I can't show custom content blocks in a loop on the home page. I'm open to any plugins or suggestions. Thanks!

TL;DR I'd like to create content blocks, in posts, that can be reused anywhere, including loops on the homepage.

Upvotes: 1

Views: 1448

Answers (1)

aharris88
aharris88

Reputation: 3630

You could use includes. Just create a file like _includes/test.html:

This is the random post content

And then put this wherever you want it to be included:

{% include test.html %}

Upvotes: 2

Related Questions