Adding author biographies to articles

Imagine for a moment that multiple people contribute to your site's blog. In return for providing you with content, authors expect a bit of publicity and a link back to their own site from the bottom of their article.

Once a blogger has written a couple of posts for you you'll find yourself wanting to re-use their biography, photo and links on each of their articles. What's the best way to do this with Nesta?

There are a couple of approaches you can take, but I really like the way that Adam and Wynn are doing it on thesassway.com.

Here's what you do (as Adam explained it to me):

  1. Add some custom metadata to each blog post that identifies the author.
  2. Create a Haml template for each author in your ./views folder that contains the HTML for their bio, link and photo.
  3. Update your page template (e.g. views/page.haml) and get it to check whether the page has an author. If an author is defined, render the relevant author's bio.

So your blog post might look this:

Author: Adam Stacoviak
Date: 28 August 2011
Categories: adam-stacoviak, projects
Summary: Twitter’s “Bootstrap” is a HOT topic…

# Sass Twitter Bootstrap

Twitter’s “Bootstrap” is a HOT topic…

Then add the markup for the author's bio to a new template in the views folder. Give the template a name that can be derived from the author's name. I'd recommend converting it to lower case and replacing spaces with underscores, to give views/adam_stacoviak.haml.

Let's add a custom helper to app.rb that will render our author's template for us. Define author_biography in your app.rb file:

module Nesta
  class App
    def author_biography(name = nil)
      name ||= @page.metadata('author')
      if name
        template = name.downcase.gsub(/\W+/, '_').to_sym
        haml template, :layout => false
      end
    end
  end
end

In your views/page.haml file add a snippet of Haml where you'd like to display the bio (it might make sense to insert it after the line that calls @page.to_html):

= author_biography

If you're wondering what that optional name argument to author_biography is there for, read on...

Create a page for each author

If you take another look at the metadata for the example post above you'll see that the post has been added to the adam-stacoviak category. That means that it will automatically be listed on a page at the URL /adam-stacoviak. Wouldn't it be nice if you include the bio at the top of that page? The easiest way to do it would be to create /adam-stacoviak as a Haml page in content/pages and include the biography like this:

Category: authors

%h1 Adam Stacoviak

= author_biography('Adam Stacoviak')

Nesta will automatically insert all the pages in the author's category for you, beneath the bio.

Graham Ashton is the author and maintainer of Nesta CMS. He lives in Manchester where he's building a simpler app for tracking agile projects.

He writes a technical blog at http://effectif.com and can be found at @grahamashton on Twitter.