title | date | layout |
---|---|---|
Test post |
2015-03-10 11:32:13 -0400 |
standard |
This is one paragraph.
{% myblock %} This paragraph is in a custom block. {% endmyblock %}
Third paragraph!
title | date | layout |
---|---|---|
Test post |
2015-03-10 11:32:13 -0400 |
standard |
This is one paragraph.
{% myblock %} This paragraph is in a custom block. {% endmyblock %}
Third paragraph!
--- | |
layout: null | |
--- | |
<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
{% for post in site.posts %} | |
<h2>{{ post.title }}</h2> | |
<p>The block is "{{ post.my_block_contents }}"</p> | |
{% endfor %} | |
</body> | |
</html> |
class MyBlock < Liquid::Block | |
def render(context) | |
# Get the current post's post object | |
id = context['page']['id'] | |
posts = context.registers[:site].posts | |
post = posts[posts.index {|post| post.id == id}] | |
contents = super | |
post.data['my_block_contents'] = contents | |
contents | |
end | |
end | |
Liquid::Template.register_tag('myblock', MyBlock) |
<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
{{ content }} | |
<hr/> | |
{% if page.my_block_contents %} | |
{{ page.my_block_contents }} | |
{% else %} | |
no block contents | |
{% endif %} | |
</body> | |
</html> |