Erb Output Buffering

January 14th, 2007

For those of you that want some painless output buffering in Rails (when using ERB templates), I have a plugin that may help you


./script/plugin install svn://ahgsoftware.com/erb_buffer/trunk

It handles unlimited nesting of buffers, and its fairly simple to use and extend. Lemme know how you guys like it. I see plenty of people downloading but no ones commenting! Any feedback for improving them would be greatly appreciated!

Here’s an example of single buffering

1
2
3
4
5
<% @contents = capture_buffer do  %>
Buffered Content, show me later in green!
<% end %>

<p style="color: green;"><%= @contents %></p>

And here is a nesting example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<% @contents_surface = capture_buffer do  %>
        I'm heavy on top, like an inverted pyramid!
        <% @contents_shallow = capture_buffer do %>
                Only Shallow Hal would occupy this plane of existence.
                <% @contents_deep = capture_buffer do %>
                        deeper deeper
                <% end %>
        <% end %>
<% end %>

<p>ending nested buffers</p>

<%= @contents_surface %>
<%= @contents_shallow %>
<%= @contents_deep %>

I must say, that, I do not like the mvc(h) setup in Rails very much. For me, the ORM is nearly a work of art, and Rails itself is certainly a very well thought out glue for the components. I think it suffers from being too small for HUGE enterprise uses, and too large for simple 1-10 file scripts that could easily handle some simple webtask for you. I’m starting to discover that a microframework like Camping might suit 40% of my projects much better than a fullblow Rails installation, and use of captured buffers, akin to PHP’s ob_* methods, would make much more sense. I’ll let ya know when I get a production site up that breaks the Rails mold!

Leave a Reply

 

Michael Cerna Chicago-based Rails Developer and Avid Musician. More ...

Search

Categories

  • Home (15)
  • Rails Plugins (5)
  • Pages (9)
  • Archives

    Tags

    BlogRoll