HOWTO: Getting a sidebar in Merb

Published:
Updated:
Article source on GitHub

In several of my pages, I have a side-bar menu-y thingie. I didn't want to have to rewrite a controller-specific layout each time, but luckily Merb supports something similar to Rails's content_for block that I wrote about earlier. In Merb, its done using throw_content(API) and catch_content(API).

Put the catch_content into your application layout view. You probably already have catch_content :for_layout in there, by default. Here's what mine looks like:

%html
  %head
    %meta{:'http-equiv' => 'content-type', 'content' => 'application/xhtml+xml; charset=UTF-8'}

    = css_include_tag "layout", "style"

    %title Page with Sidebar

  %body

    #side-bar
      = catch_content :sidebar

    #main
      = catch_content :for_layout

    #footer
      .left= copyright
      .right= last_modified

Using haml, I've put my sidebar in a div with id #side-bar.

Now in the view, add a throw_content for what you want in the sidebar. In my case, I'm using a partial that gets picked up out of the controller's view directory automatically.

- throw_content(:sidebar, partial('sidebar'))

%h1 This page has a sidebar

And ta-da! I only have to write the sidebar partial once for each controller, and I don't have to write an extra layout for each one. I have a fairly uncomplicated layout, and fill out the various parts of it by throwing rendered partials into it.