Passing ERB block to helper

I’m trying to pass an ERB block to a helper, and I’m not able to achieve
the results I want. The helper should take the block and incorporate it
into some other text & html, before finally outputing it back to the ERB
template.

Here’s what I want:

–helper–

def helper (a, b)
yld = yield
out = --some function of a, b, and yld–
out
end

–ERB–

<% helper(‘foo’, ‘bar’) do %>
–html mixed with erb–
<% end %>

If I include the equal in <%=, I get errors. If I drop the equal, I lose
the output of the helper and just get the raw yield. I’ve tried every
possible combination of syntax here with no luck. Any thoughts?

wbr

On 10/6/07, Bill R. [email protected] wrote:

def helper (a, b)

If I include the equal in <%=, I get errors. If I drop the equal, I lose
the output of the helper and just get the raw yield. I’ve tried every
possible combination of syntax here with no luck. Any thoughts?

I’m not an expert on this by any means, but something along these
lines should work:

def helper(a, b, &block)
yld = capture(&block)
out = …build up string using a, b, and yld…
concat out, block.binding
end

Use “<% helper”, not “<%= helper” when you call it (no equal sign)

HTH

Bob S. wrote:

I’m not an expert on this by any means, but something along these
lines should work:

def helper(a, b, &block)
yld = capture(&block)
out = …build up string using a, b, and yld…
concat out, block.binding
end

Use “<% helper”, not “<%= helper” when you call it (no equal sign)

HTH

Ahh… I suspected it had something to do with ‘capture’ and ‘concat’.
Your suggestion works, when I try my scaled down demo solution, which is
a huge step forward. But my full-blown helper still bombs. Anyone know
of a good resource on the finer workings of capture and concat? Or more
generally on the interactions between controllers, helpers, and
templates? I have books, and I’ve scoured the web, yet I’ve found very
little documentation on this topic.

Thanks

Ok, got it. I had a piece of bad code in the helper, and the error
produced looked like a problem with the block/capture/concat stuff.
Thanks for the help. Here’s what works:

–erb–

<% build_test(args, *options, &block) do %>
some rhtml here
<%# end %>

–helper–

def build_test (args, *options, &block)
x = args.to_s
y = capture(&block) if block_given?
y += x
y += “…some more text”
concat z, block.binding
end

WBR

… er um, sorry, the z should be a y.

wbr