HOW I: Wrap content sections using blocks and then nest them using lambdas

This is the type of code that I’m excited when it works but hopefully will get some suggestions on how to do it better. Last time, the code was a bit to “ferocious” and I was missing out on a built in feature to rails that I didn’t know about.

This one is a bit more complex… to factor out all the markup for different sections of the site I have a bunch of view helpers (thanks to Ryan Bates: http://railscasts.com/episodes/40 ) that are setup to take blocks. Most of my views start off like this:

<% content_for :left do %> <% conversations_area do %>

<% end %>

<% end %>

This makes is tremendously easy to DRY up repeated code that doesn’t really warrant its own layout bit is still fairly consistent.

To complicate this, however, we have different sections of the site that are scoped to a specifc group. For example, a group could have its own conversations area. Using RESTful routes I’m reusing all the same controllers and views for both. Now the problem is, if I’m at the group level I need to use the group’s view helper. Using lambdas and blocks this is pretty easy:

<% content_for :left do %>

<!-- Define the content to pass to the view helper -->
<% content = lambda do %>
<% end %>

<!-- Determine which view helper to pass the block to -->

<% if @group %>
    <% groups_area(@group) { content.call } %>
<% else %>
    <% conversations_area(@forum,@group) { content.call } %>
<% end %>

<% end %>

So far this is working great. Any ideas on how to improve it? Bonus question:

Why don’t here documents work in the helper files?

Nathan http://www.blenderbox.com