Interesting idea

Hey guys,

There is something I have been trying to implement but so far no luck.
Basically, I want to have helpers that generate HTML around given
content, similar to ActionView’s form_for. Besides, I want to keep
that “framing” HTML in separate files, similar to templates.

Ideally I’d even love to suggest it as a feature for Rails. Let me
show you an example.
This is what I want to have in my rhtml file:

<% bubble_block :corners => “round”, :color => “brown” do %>

My content
<% end %>

So far I have tried the following: I added a method to my application
helper:

def bubble_block(options, &proc)
raise ArgumentError, “No block given” unless block_given?
rendered_html = render(:partial => “/templates/bubble”, :locals =>
{:block => proc} )
concat rendered_html, proc.binding
end

This is how _bubble.rhtml looks like:

<% block.call %>

What happens is ALMOST what I want, except my block gets rendered
twice. The output looks like this:

My content
My content

What am I missing? Thank you!

On May 2, 2007, at 2:26 PM, zeka wrote:

{:block => proc} )
twice. The output looks like this:

My content
My content

What am I missing? Thank you!

Since you don’t say what options is supposed to do, perhaps this give
you an idea of how to proceed:

def bubble_block options={}, &block
raise ArgumentError, “No block given” unless block_given?
content_tag :div, {:id => ‘bubble’}.merge(options), &block
end

and forget about the unnecessary _bubble.rhtml

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thank you for replying, Rob.

I already know how to do it without external .rhtml (I can use
concat() inside of bubble_for). However, the entire idea is about
using external .rhtml files for surrounding HTML, since there is a lot
of HTML and having that HTML embedded into ruby code is not practical
(designers need to touch it).

What exactly in my implementation causes the wrapped block to render
twice? Inside and outside of the surrounding HTML?