When setting up a new blog within Wheelhouse CMS, you may be surprised to see somewhat of a minimalist picture when it comes to features such as blogrolls, custom banner images and post excerpts. In actual fact, the powerful templating features of Wheelhouse CMS not only make adding these features to your blog delightfully simple, but also pave the way for a huge amount of customization possibilities.

In this article I'll show you how to add excerpts to your site's blog posts, a feature I've recently incorporated into a couple of my own clients' blogs.

Customizing the post template

The Wheelhouse blog plugin allows you to customize the post template for all views by overriding a single partial. To define a very basic post template, save the following as blog/_post.html.erb within your theme's templates folder:

<article class="post">
<h3><%= post.title %></h3>
<p class="date"><%= post.published_at.to_s(:short) %></p>

<%= post.content %>
</article>

This template will be rendered for each post on the blog index (as well as archives, tags and category pages), as well as when viewing the post in isolation.

The current_page?(post) Rails helper can be used to determine if the user is on a post or index page. Other built-in Rails helpers can also be used, such as link_to_unless_current to only link the post title from the blog index pages.

Adding a content field for excerpts

The other important piece of the puzzle is how to define the post excerpts so that they can be edited by the post author. Wheelhouse's content area helpers give us a huge amount of flexibility here, allowing us to define editable content areas, text fields, images or a combination of these.

To define a short excerpt field, which will appear below the main post content field when editing the post, we can use the following line in the template:

<%= post.content "Excerpt", :rows => 5 %>

Bringing it all together

Combining these concepts, we end up with the following post template:

<article class="post">
<h3><%= link_to_unless_current post.title, post %></h3>
<p class="date"><%= post.published_at.to_s(:short) %></p>

<% if current_page?(post) %>
<%= post.content %>
<% else %>
<%= post.content "Excerpt", :rows => 5 %>
<%= link_to "Read More", post %>
<% end %>
</article>

Which looks like this when editing the post within the Wheelhouse admin:

Blog Post Excerpts

Bonus: automatic excerpts

These fully custom excerpts give the post author complete control as to how the post excerpt should appear. However it requires the post author explicitly enter in the excerpt content, usually by copy and pasting the first few sentences of the post. If we are aren't fussy about the format of the excerpts and just care about their length, we can simply truncate the post content and get the excerpt for free.

Although Rails provides a built-in truncate helper, this unfortunately does not work on HTML output such as the post content. Instead we can use the truncate_html helper by adding it to the Gemfile and using the following code in the post template to produce the excerpt:

<%= truncate_html(post.content, :length => 300) %>
<%= link_to "Read More", post %>