Contentful Middleman is a Middleman extension to use the Middleman static site generator together with the API-driven Contentful CMS. It is powered by the Contentful Ruby Gem.
Experience the power of Middleman while staying sane as a developer by letting end-users edit content in a web-based interface.
This extensions supports both page-based content as well as blog posts through middleman-blog.
Add the following line to the Gemfile of your Middleman project:
gem "contentful_middleman"
Then as usual, run:
bundle install
To configure the extension, add the following configuration block to Middleman's config.rb:
activate :contentful do |f|
# The Space ID of your Contentful space
f.space = 'YOUR_SPACE_ID'
# The access token (API Key) for the Content Delivery API
f.access_token = 'YOUR_CONTENT_DELIVERY_API_ACCESS_TOKEN'
# Optional: Options for middleman-blog
# Filter Entries for your blog posts. See Contentful gem and Content Delivery API documentation.
f.blog_posts_query = {content_type: "6LbnqgnwA08qYaU", category: "news" }
# Which keys to use in the article template for blog posts
# Key: template variable
# Value: Entry method or block
f.blog_post_mappings = {
slug: :id,
date: :created_at,
body: :id,
tags: :tags,
title: ->(e){"#{e.id}XXXX"}
}
# Define your own template for blog posts
f.new_article_template = "/my_templates/article.tt"
# Automatically synchronize blog posts before building with "middleman build"
f.sync_blog_before_build = true # default: false
end
The contentful
helper provides a Contentful gem client object, that can be used to fetch managed content from Contentful:
<ol>
<% contentful.entries(content_type: '6LbnqgnwA08qYaU').each do |entry| %>
<li>
<%= entry.title %>
<%= entry.body %>
<%= entry.created_at %>
</li>
<% end %>
</ol>
If you want to use markdown in your content types you manually have to render this to markdown. Depending on the markdown library you need to transform the data. For Kramdown this would be:
<%= Kramdown::Document.new(entry.body).to_html %>
Blog posts are synchronized to your repo as YAML files with front matter, just as if you would write them manually. Either automatically when building, or manually by running:
middleman contentful