jekyll-contentblocks
Gives you a mechanism in Jekyll to pass content up from pages into their parent layouts. It's kind of like having Rails' content_for available for Jekyll.
Installation
Bundler (recommended)
Add this line to your Jekyll project's Gemfile
:
gem 'jekyll-contentblocks'
Then execute:
$ bundle install
Make sure your have a plugin that initializes Bundler:
# _plugins/bundler.rb
require "rubygems"
require "bundler/setup"
Bundler.require(:default)
Standalone
Execute:
$ gem install jekyll-contentblocks
And initialize it in a plugin:
# _plugins/ext.rb
require "rubygems"
require "jekyll-contentblocks"
Usage
In your layout files, define contentblock
blocks that say where content will end up. For example, say the file _layouts/default.html
looks like this:
<html>
<head>
{% contentblock scripts %}
</head>
<body>
<div class="main">
{{ content }}
</div>
<div class="sidebar">
{% contentblock sidebar %}
</div>
</body>
</html>
Now to add content to the sidebar from a post, you'd just need to do something like:
---
layout: default
---
Here is my post content.
{% contentfor sidebar %}
* Some content
* in a markdown list
* with some {{ 'liquid' }} tags too!
{% endcontentfor %}
Note that we didn't add anything to the scripts
block in the post. That's OK, content blocks without any content will be ignored.
Checking if a block has content
We might want to check if the particular contentblock has content before using it in our template.
To do this, use the ifhascontent
tag:
{% ifhascontent javascripts %}
<script type="text/javascript>
{% contentfor javascripts %}
</script>
{% endifhascontent %}
Similarly, there's the opposite tag, ifnothascontent
:
{% ifnothascontent sidebar %}
<div>
This is our default sidebar.
</div>
{% endifnothascontent %}
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Running the tests
Try to make sure that your changes work with all of the latest point releases of Jekyll. To do this, run the test suite:
> bundle
> bundle exec appraisal install
> bundle exec appraisal rpsec