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

**URL:** <https://www.ruby-forum.com/t/how-i-wrap-content-sections-using-blocks-and-then-nest-them/108566>\
**Category:** Rails\
**Created:** [July 4, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/how-i-wrap-content-sections-using-blocks-and-then-nest-them/108566 "2007-07-04T19:47:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [July 4, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/how-i-wrap-content-sections-using-blocks-and-then-nest-them/108566/1 "2007-07-04T19:47:40Z")

</div>

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:  
[#40 Blocks in View - RailsCasts](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 %>
    <!-- main content here -->
<% 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

> **[Blenderbox – A human-centered design and digital product agency](https://blenderbox.com/)**
>
> We help governments and civic change-makers build trust and impact through human-centered design systems and services.
