1
votes

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.

1
Do you want to create custom content blocks, where they only contain specific blocks of text, or you just want to grab the entire contents of a post (i.e all the markdown text there without the YAML)? If it's the second one, {{ post.content }} works beautifully. Else you probably have to do front matter method (I'll post an answer once I get this clarification) :)matrixanomaly
My thought is, I can hand it off to someone savvy, but has less programming knowledge than me, and they can figure out that content_block_one shows in a specific spot. I guess I'd like to have designated blocks that can show anywhere similar to my example. In the post define a block of content, just markdown and in the template, use it.Greg Thompson
I see, so you have a chunk of markdown that will be split into e.g content_block_one, two, etc, inserted in the layout differently. I guess the way to go with that is using captures like what you did, or else the default is only {{ post.content }} which is all text other than the frontmatter, I used it to inject the content in a specific div.matrixanomaly

1 Answers

2
votes

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 %}